Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# ✅ 1. Load GPT-Neo model
|
| 6 |
+
model_name = "EleutherAI/gpt-neo-1.3B"
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
| 11 |
+
|
| 12 |
+
# ✅ 2. Chatbot-style Text Generator
|
| 13 |
+
def chat_with_gptneo(history, user_input):
|
| 14 |
+
# Combine chat history for context (optional, keep recent exchanges)
|
| 15 |
+
if history:
|
| 16 |
+
past = "\n".join([f"User: {msg[0]}\nBot: {msg[1]}" for msg in history])
|
| 17 |
+
prompt = f"{past}\nUser: {user_input}\nBot:"
|
| 18 |
+
else:
|
| 19 |
+
prompt = f"User: {user_input}\nBot:"
|
| 20 |
+
|
| 21 |
+
inputs = tokenizer(prompt, return_tensors='pt').to(device)
|
| 22 |
+
|
| 23 |
+
outputs = model.generate(
|
| 24 |
+
inputs['input_ids'],
|
| 25 |
+
max_new_tokens=200,
|
| 26 |
+
do_sample=True,
|
| 27 |
+
temperature=0.85,
|
| 28 |
+
top_k=50,
|
| 29 |
+
top_p=0.95,
|
| 30 |
+
repetition_penalty=1.2,
|
| 31 |
+
no_repeat_ngram_size=3,
|
| 32 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 33 |
+
early_stopping=True
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
+
response = full_output.replace(prompt, "").strip()
|
| 38 |
+
return response
|
| 39 |
+
|
| 40 |
+
# ✅ 3. Gradio Chat Interface
|
| 41 |
+
with gr.Blocks(title="GPT-Neo Chatbot") as demo:
|
| 42 |
+
gr.Markdown("## 🤖 GPT-Neo 1.3B Chatbot\nAsk me anything!")
|
| 43 |
+
|
| 44 |
+
chatbot = gr.Chatbot()
|
| 45 |
+
msg = gr.Textbox(placeholder="Type your prompt here...", label="Your Prompt")
|
| 46 |
+
clear = gr.Button("Clear")
|
| 47 |
+
|
| 48 |
+
history = gr.State([])
|
| 49 |
+
|
| 50 |
+
def respond(user_message, chat_history):
|
| 51 |
+
bot_message = chat_with_gptneo(chat_history, user_message)
|
| 52 |
+
chat_history.append((user_message, bot_message))
|
| 53 |
+
return "", chat_history
|
| 54 |
+
|
| 55 |
+
msg.submit(respond, [msg, history], [msg, chatbot])
|
| 56 |
+
clear.click(lambda: ([], []), None, [history, chatbot])
|
| 57 |
+
|
| 58 |
+
# ✅ 4. Launch the app
|
| 59 |
+
demo.launch()
|