Lolipopking commited on
Commit
02e9fe5
·
verified ·
1 Parent(s): 44e272a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -1,28 +1,31 @@
1
- import torch
2
  import gradio as gr
 
3
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4
 
5
- model_name = "tiiuae/falcon-rw-1b"
6
 
7
- # Load tokenizer and model
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto")
10
 
11
- # Create text generation pipeline
12
  generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
13
 
14
- # Chat function (ChatGPT-style with faster, shorter replies)
15
  def chat(user_input, history=[]):
16
- # Simple system prompt
17
- system_prompt = (
18
- "You are a helpful, smart, and friendly assistant. "
19
- "Reply politely, clearly, and concisely like ChatGPT."
20
- )
21
-
22
- # Build conversation input
23
- full_input = system_prompt + "\n"
24
- for turn in history:
25
- full_input += f"User: {turn[0]}\nAI: {turn[1]}\n"
26
- full_input += f"User: {user_input}\nAI:"
27
-
28
- # Generate reply (
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4
 
5
+ model_name = "sshleifer/tiny-gpt2" # Light model for free CPU Space
6
 
 
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto")
9
 
 
10
  generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
 
 
12
  def chat(user_input, history=[]):
13
+ prompt = "You are a helpful and friendly assistant.\n"
14
+ for msg in history:
15
+ prompt += f"User: {msg[0]}\nAI: {msg[1]}\n"
16
+ prompt += f"User: {user_input}\nAI:"
17
+
18
+ response = generator(prompt, max_new_tokens=60, do_sample=True, temperature=0.7)[0]["generated_text"]
19
+ reply = response.split("AI:")[-1].split("User:")[0].strip()
20
+ history.append((user_input, reply))
21
+ return history, history
22
+
23
+ with gr.Blocks() as demo:
24
+ gr.Markdown("# 🤖 Tiny GPT-2 Chatbot")
25
+ chatbot = gr.Chatbot()
26
+ msg = gr.Textbox(label="Ask something...")
27
+ state = gr.State([])
28
+
29
+ msg.submit(chat, [msg, state], [chatbot, state])
30
+
31
+ demo.launch()