Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,62 @@
|
|
| 1 |
import time
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
# --------------------------
|
| 7 |
-
# Load
|
| 8 |
# --------------------------
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
-
model_name,
|
| 14 |
-
device_map="auto" if torch.cuda.is_available() else None, # GPU if available
|
| 15 |
-
dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 16 |
)
|
| 17 |
|
| 18 |
# --------------------------
|
| 19 |
-
# Chat history
|
| 20 |
# --------------------------
|
| 21 |
chat_history = []
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Combine previous conversation
|
| 25 |
context = ""
|
| 26 |
-
for user_msg, ai_msg in
|
| 27 |
context += f"User: {user_msg}\nAI: {ai_msg}\n"
|
| 28 |
context += f"User: {prompt}\nAI:"
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
-
response = response.split("AI:")[-1].strip()
|
| 38 |
return response
|
| 39 |
|
| 40 |
# --------------------------
|
| 41 |
-
#
|
| 42 |
# --------------------------
|
| 43 |
def live_typing(prompt):
|
| 44 |
-
|
| 45 |
-
response = generate_response(prompt, chat_history)
|
| 46 |
displayed_text = ""
|
| 47 |
for char in response:
|
| 48 |
displayed_text += char
|
| 49 |
-
time.sleep(0.02) #
|
| 50 |
yield displayed_text
|
| 51 |
-
# Update chat history
|
| 52 |
-
chat_history.append((prompt, response))
|
| 53 |
|
| 54 |
# --------------------------
|
| 55 |
# Gradio UI
|
| 56 |
# --------------------------
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
-
gr.Markdown("## 🤖
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
with gr.Column(scale=1):
|
| 62 |
user_avatar = gr.Image("user_avatar.png", elem_id="user-avatar")
|
| 63 |
with gr.Column(scale=4):
|
| 64 |
user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
|
| 65 |
-
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column(scale=1):
|
| 68 |
ai_avatar = gr.Image("ai_avatar.png", elem_id="ai-avatar")
|
|
|
|
| 1 |
import time
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
|
| 5 |
# --------------------------
|
| 6 |
+
# Load CPU-friendly 4B model
|
| 7 |
# --------------------------
|
| 8 |
+
llm = Llama.from_pretrained(
|
| 9 |
+
repo_id="DavidAU/Gemma-3-it-4B-Uncensored-DBL-X-GGUF",
|
| 10 |
+
filename="Gemma-3-it-4B-Uncensored-D_AU-F16.gguf",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
# --------------------------
|
| 14 |
+
# Chat history
|
| 15 |
# --------------------------
|
| 16 |
chat_history = []
|
| 17 |
|
| 18 |
+
# --------------------------
|
| 19 |
+
# Generate response
|
| 20 |
+
# --------------------------
|
| 21 |
+
def generate_response(prompt):
|
| 22 |
+
global chat_history
|
| 23 |
# Combine previous conversation
|
| 24 |
context = ""
|
| 25 |
+
for user_msg, ai_msg in chat_history:
|
| 26 |
context += f"User: {user_msg}\nAI: {ai_msg}\n"
|
| 27 |
context += f"User: {prompt}\nAI:"
|
| 28 |
|
| 29 |
+
# Generate text
|
| 30 |
+
output = llm(prompt=context, max_tokens=200)
|
| 31 |
+
response = output['choices'][0]['text'].strip()
|
| 32 |
+
|
| 33 |
+
# Update history
|
| 34 |
+
chat_history.append((prompt, response))
|
|
|
|
|
|
|
| 35 |
return response
|
| 36 |
|
| 37 |
# --------------------------
|
| 38 |
+
# Live typing simulation
|
| 39 |
# --------------------------
|
| 40 |
def live_typing(prompt):
|
| 41 |
+
response = generate_response(prompt)
|
|
|
|
| 42 |
displayed_text = ""
|
| 43 |
for char in response:
|
| 44 |
displayed_text += char
|
| 45 |
+
time.sleep(0.02) # typing speed
|
| 46 |
yield displayed_text
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# --------------------------
|
| 49 |
# Gradio UI
|
| 50 |
# --------------------------
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("## 🤖 Gemma-3 Chatbot (CPU-Friendly) with Avatars and Live Typing")
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column(scale=1):
|
| 56 |
user_avatar = gr.Image("user_avatar.png", elem_id="user-avatar")
|
| 57 |
with gr.Column(scale=4):
|
| 58 |
user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
|
| 59 |
+
|
| 60 |
with gr.Row():
|
| 61 |
with gr.Column(scale=1):
|
| 62 |
ai_avatar = gr.Image("ai_avatar.png", elem_id="ai-avatar")
|