File size: 3,997 Bytes
785e38d 9bc4c0b 785e38d 1d7a76e 9bc4c0b 1d7a76e 9bc4c0b 1444cb4 9bc4c0b 785e38d 1d7a76e 73233ea 1d7a76e 73233ea 1444cb4 785e38d 73233ea 785e38d 9bc4c0b 1444cb4 73233ea 785e38d 1d7a76e 9bc4c0b 73233ea 1d7a76e 73233ea 785e38d 73233ea 785e38d 1d7a76e 785e38d 1d7a76e 785e38d 73233ea 1d7a76e 785e38d 73233ea 785e38d 1d7a76e 785e38d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import time
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
# --------------------------
# Load model
# --------------------------
model_name = "Fredithefish/Guanaco-3B-Uncensored-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
# --------------------------
# Chat history
# --------------------------
chat_history = []
# --------------------------
# Generate response
# --------------------------
def generate_response(prompt):
global chat_history
# Combine previous conversation
context = ""
for user_msg, ai_msg in chat_history:
context += f"User: {user_msg}\nAI: {ai_msg}\n"
context += f"User: {prompt}\nAI:"
inputs = tokenizer(context, return_tensors="pt").to(device)
outputs = model.generate(
**inputs,
max_new_tokens=150,
do_sample=True,
temperature=0.7
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("AI:")[-1].strip()
chat_history.append((prompt, response))
return response
# --------------------------
# Live typing effect
# --------------------------
def live_typing(prompt):
response = generate_response(prompt)
displayed_text = ""
for char in response:
displayed_text += char
time.sleep(0.02) # typing speed
yield displayed_text
# --------------------------
# Gradio UI
# --------------------------
with gr.Blocks() as demo:
gr.Markdown("## π€ Guanaco-3B Chatbot with Live Typing")
# User input
user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
# Output box
output_box = gr.Textbox(label="AI is typing...", lines=8)
# Submit event
user_input.submit(live_typing, inputs=[user_input], outputs=[output_box])
demo.launch()
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("AI:")[-1].strip()
chat_history.append((prompt, response))
return response
# --------------------------
# Simulate live typing
# --------------------------
def live_typing(prompt):
response = generate_response(prompt)
displayed_text = ""
for char in response:
displayed_text += char
time.sleep(0.02) # typing speed
yield displayed_text
# --------------------------
# Gradio UI
# --------------------------
with gr.Blocks() as demo:
gr.Markdown("## π€ Guanaco-3B Chatbot with Avatars and Live Typing")
with gr.Row():
with gr.Column(scale=1):
user_avatar = gr.Image("user_avatar.png", elem_id="user-avatar")
with gr.Column(scale=4):
user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
with gr.Row():
with gr.Column(scale=1):
ai_avatar = gr.Image("ai_avatar.png", elem_id="ai-av_
# --------------------------
# Live typing simulation
# --------------------------
def live_typing(prompt):
response = generate_response(prompt)
displayed_text = ""
for char in response:
displayed_text += char
time.sleep(0.02) # typing speed
yield displayed_text
# --------------------------
# Gradio UI
# --------------------------
with gr.Blocks() as demo:
gr.Markdown("## π€ Gemma-3 Chatbot (CPU-Friendly) with Avatars and Live Typing")
with gr.Row():
with gr.Column(scale=1):
user_avatar = gr.Image("user_avatar.png", elem_id="user-avatar")
with gr.Column(scale=4):
user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
with gr.Row():
with gr.Column(scale=1):
ai_avatar = gr.Image("ai_avatar.png", elem_id="ai-avatar")
with gr.Column(scale=4):
output_box = gr.Textbox(label="AI is typing...", lines=8)
user_input.submit(live_typing, inputs=[user_input], outputs=[output_box])
demo.launch()
|