Spaces:
No application file
No application file
File size: 1,322 Bytes
2365310 c64ad49 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
# Choose a lightweight, open model
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=256,
do_sample=True,
temperature=0.7,
top_p=0.9
)
def chat(history, message):
# Build conversation text
prompt = ""
for user, bot in history:
prompt += f"User: {user}\nAssistant: {bot}\n"
prompt += f"User: {message}\nAssistant:"
output = pipe(prompt)[0]["generated_text"]
reply = output.split("Assistant:")[-1].strip()
history.append((message, reply))
return history, ""
with gr.Blocks() as demo:
gr.Markdown("# 🔥 My Chatbot")
chatbot = gr.Chatbot()
msg = gr.Textbox(label="Say something")
clear = gr.Button("Clear chat")
state = gr.State([])
def respond(message, history):
if history is None:
history = []
return chat(history, message)
msg.submit(respond, [msg, chatbot], [chatbot, msg])
clear.click(lambda: ([], ""), None, [chatbot, msg])
demo.launch()
|