PROWarrior commited on
Commit
f6d4c98
·
verified ·
1 Parent(s): cdb0d75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -1,23 +1,21 @@
 
1
  import gradio as gr
2
- from transformers import pipeline, set_seed
3
 
 
4
  generator = pipeline("text-generation", model="sshleifer/tiny-gpt2")
5
- set_seed(42)
6
 
7
- def chat_fn(message):
8
- result = generator(message, max_length=60, num_return_sequences=1)
9
- reply = result[0]["generated_text"].replace(message, "").strip()
 
10
  return reply
11
 
12
- # Use Blocks for better layout control
13
- with gr.Blocks(css="body {margin-top: 40px;}") as demo:
14
- gr.Markdown("# 💬 GPT-2 Chatbot")
15
- gr.Markdown("Talk to a small GPT-2 model powered by Transformers and Gradio.")
16
- with gr.Row():
17
- with gr.Column(scale=4):
18
- user_input = gr.Textbox(placeholder="Type your message...", label="You")
19
- output = gr.Textbox(label="Bot")
20
- btn = gr.Button("Send")
21
- btn.click(fn=chat_fn, inputs=user_input, outputs=output)
22
 
23
- demo.launch()
 
1
+ from transformers import pipeline
2
  import gradio as gr
 
3
 
4
+ # Load GPT-2 (or use sshleifer/tiny-gpt2 for low RAM)
5
  generator = pipeline("text-generation", model="sshleifer/tiny-gpt2")
 
6
 
7
+ def chat_fn(message, history=[]):
8
+ prompt = message.strip()
9
+ result = generator(prompt, max_length=60, num_return_sequences=1)
10
+ reply = result[0]['generated_text'].replace(prompt, "").strip()
11
  return reply
12
 
13
+ demo = gr.ChatInterface(
14
+ fn=chat_fn,
15
+ title="💬 GPT-2 Chatbot",
16
+ description="Powered by sshleifer/tiny-gpt2 for light, fast, local inference"
17
+ )
18
+
19
+ if __name__ == "__main__":
20
+ demo.launch(share=True)
 
 
21