Simple_Chatbot / app.py
ar0551's picture
Update app.py
edad343 verified
raw
history blame
1.24 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_id = "microsoft/phi-2"
# Load model and tokenizer (CPU + float32)
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float32
).to("cpu")
# Chat function
def chat_with_bot(user_input, history):
history = history or []
prompt = ""
for user, bot in history:
prompt += f"User: {user}\nAssistant: {bot}\n"
prompt += f"User: {user_input}\nAssistant:"
inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
outputs = model.generate(
**inputs,
max_new_tokens=256,
do_sample=True,
temperature=0.7,
top_p=0.95,
eos_token_id=tokenizer.eos_token_id,
)
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
response = decoded[len(prompt):].strip().split("\n")[0]
history.append((user_input, response))
return response, history
# Gradio UI
gr.ChatInterface(
fn=chat_with_bot,
title="Phi-2 Chatbot (ZeroGPU Safe)",
theme="soft",
examples=["What is AI?", "Summarize the French Revolution.", "Tell me a space fact."]
).launch(share=True)