DSDUDEd commited on
Commit
73233ea
·
verified ·
1 Parent(s): 1d7a76e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
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 13B GPTQ model
8
  # --------------------------
9
- model_name = "TheBloke/Wizard-Vicuna-13B-Uncensored-GPTQ"
10
-
11
- tokenizer = AutoTokenizer.from_pretrained(model_name, legacy=False)
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 & response
20
  # --------------------------
21
  chat_history = []
22
 
23
- def generate_response(prompt, history):
 
 
 
 
24
  # Combine previous conversation
25
  context = ""
26
- for user_msg, ai_msg in history:
27
  context += f"User: {user_msg}\nAI: {ai_msg}\n"
28
  context += f"User: {prompt}\nAI:"
29
 
30
- inputs = tokenizer(context, return_tensors="pt")
31
- if torch.cuda.is_available():
32
- inputs = {k: v.cuda() for k, v in inputs.items()}
33
-
34
- # Generate output
35
- outputs = model.generate(**inputs, max_new_tokens=150, do_sample=True, temperature=0.7)
36
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
37
- response = response.split("AI:")[-1].strip()
38
  return response
39
 
40
  # --------------------------
41
- # Simulate live typing
42
  # --------------------------
43
  def live_typing(prompt):
44
- global chat_history
45
- response = generate_response(prompt, chat_history)
46
  displayed_text = ""
47
  for char in response:
48
  displayed_text += char
49
- time.sleep(0.02) # Typing speed
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("## 🤖 Wizard-Vicuna 13B Chatbot with Avatars and Live Typing")
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")