jeevav62 commited on
Commit
ca1f085
Β·
verified Β·
1 Parent(s): 11e6ed2

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -2,15 +2,21 @@ import torch
2
  import gradio as gr
3
  from transformers import pipeline
4
 
 
 
 
5
  pipe = pipeline(
6
  "text-generation",
7
  model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
8
- torch_dtype=torch.float32,
9
  device_map="auto",
10
  )
11
 
12
- def chat(user_message, history, system_prompt):
13
- # Stronger personality by prepending system prompt
 
 
 
14
  messages = [{"role": "system", "content": system_prompt}]
15
 
16
  for u, a in history:
@@ -27,8 +33,8 @@ def chat(user_message, history, system_prompt):
27
 
28
  output = pipe(
29
  prompt,
30
- max_new_tokens=128,
31
- temperature=0.85, # more creative for personality
32
  top_p=0.9,
33
  do_sample=True,
34
  )
@@ -42,8 +48,9 @@ def chat(user_message, history, system_prompt):
42
  # -------------------------------
43
  with gr.Blocks(title="TinyLLaMA Chatbot") as demo:
44
  gr.Markdown("## πŸ¦™ TinyLLaMA Chatbot")
45
- gr.Markdown("Select personality to change chatbot behavior.")
46
 
 
47
  preset_prompts = {
48
  "Pirate πŸ΄β€β˜ οΈ": "You are a friendly chatbot who always responds like a pirate. Use pirate words like 'Ahoy', 'Matey', and speak in a fun, adventurous style.",
49
  "Teacher πŸ‘¨β€πŸ«": "You are a patient teacher who explains concepts clearly. Give detailed, step-by-step explanations, and use examples.",
@@ -62,30 +69,37 @@ with gr.Blocks(title="TinyLLaMA Chatbot") as demo:
62
  label="System Prompt (Editable)"
63
  )
64
 
65
- chatbot = gr.Chatbot(height=400)
66
- user_input = gr.Textbox(label="Your Message")
67
-
68
  def update_prompt(choice):
69
  return preset_prompts[choice]
70
 
71
  system_prompt_dropdown.change(update_prompt, inputs=system_prompt_dropdown, outputs=system_prompt_textbox)
72
 
 
 
 
 
 
 
 
73
  send_btn = gr.Button("Send πŸš€")
74
  clear_btn = gr.Button("Clear 🧹")
75
 
76
  send_btn.click(
77
  chat,
78
- inputs=[user_input, chatbot, system_prompt_textbox],
79
  outputs=chatbot
80
  )
81
 
82
  user_input.submit(
83
  chat,
84
- inputs=[user_input, chatbot, system_prompt_textbox],
85
  outputs=chatbot
86
  )
87
 
88
  clear_btn.click(lambda: [], outputs=chatbot)
89
 
 
 
 
90
  demo.launch(share=True)
91
-
 
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
+ # -------------------------------
6
+ # Load TinyLLaMA Model
7
+ # -------------------------------
8
  pipe = pipeline(
9
  "text-generation",
10
  model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
11
+ torch_dtype=torch.float32, # CPU-friendly
12
  device_map="auto",
13
  )
14
 
15
+ # -------------------------------
16
+ # Chat Function
17
+ # -------------------------------
18
+ def chat(user_message, history, system_prompt, temperature, max_tokens):
19
+ # Stronger personality: always prepend system prompt
20
  messages = [{"role": "system", "content": system_prompt}]
21
 
22
  for u, a in history:
 
33
 
34
  output = pipe(
35
  prompt,
36
+ max_new_tokens=max_tokens,
37
+ temperature=temperature,
38
  top_p=0.9,
39
  do_sample=True,
40
  )
 
48
  # -------------------------------
49
  with gr.Blocks(title="TinyLLaMA Chatbot") as demo:
50
  gr.Markdown("## πŸ¦™ TinyLLaMA Chatbot")
51
+ gr.Markdown("Select personality to change chatbot behavior and adjust generation settings.")
52
 
53
+ # Predefined personalities
54
  preset_prompts = {
55
  "Pirate πŸ΄β€β˜ οΈ": "You are a friendly chatbot who always responds like a pirate. Use pirate words like 'Ahoy', 'Matey', and speak in a fun, adventurous style.",
56
  "Teacher πŸ‘¨β€πŸ«": "You are a patient teacher who explains concepts clearly. Give detailed, step-by-step explanations, and use examples.",
 
69
  label="System Prompt (Editable)"
70
  )
71
 
72
+ # Update textbox when dropdown changes
 
 
73
  def update_prompt(choice):
74
  return preset_prompts[choice]
75
 
76
  system_prompt_dropdown.change(update_prompt, inputs=system_prompt_dropdown, outputs=system_prompt_textbox)
77
 
78
+ chatbot = gr.Chatbot(height=400)
79
+ user_input = gr.Textbox(label="Your Message")
80
+
81
+ # Generation sliders
82
+ temperature_slider = gr.Slider(0.1, 1.2, value=0.85, step=0.05, label="Temperature")
83
+ max_tokens_slider = gr.Slider(32, 256, value=128, step=16, label="Max Tokens")
84
+
85
  send_btn = gr.Button("Send πŸš€")
86
  clear_btn = gr.Button("Clear 🧹")
87
 
88
  send_btn.click(
89
  chat,
90
+ inputs=[user_input, chatbot, system_prompt_textbox, temperature_slider, max_tokens_slider],
91
  outputs=chatbot
92
  )
93
 
94
  user_input.submit(
95
  chat,
96
+ inputs=[user_input, chatbot, system_prompt_textbox, temperature_slider, max_tokens_slider],
97
  outputs=chatbot
98
  )
99
 
100
  clear_btn.click(lambda: [], outputs=chatbot)
101
 
102
+ # -------------------------------
103
+ # Launch
104
+ # -------------------------------
105
  demo.launch(share=True)