Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
|
4 |
+
# Load the Mistral-7B-v0.2 model and tokenizer
|
5 |
+
model_name = "mistralai/Mistral-7B-v0.2"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Define the chatbot function
|
10 |
+
def chatbot(input_text, history=[]):
|
11 |
+
# Combine history and current input
|
12 |
+
chat_history = " ".join([f"User: {h[0]} Assistant: {h[1]}" for h in history])
|
13 |
+
input_prompt = f"{chat_history} User: {input_text} Assistant:"
|
14 |
+
inputs = tokenizer(input_prompt, return_tensors="pt", truncation=True)
|
15 |
+
|
16 |
+
# Generate response
|
17 |
+
outputs = model.generate(
|
18 |
+
**inputs,
|
19 |
+
max_length=512,
|
20 |
+
do_sample=True,
|
21 |
+
temperature=0.7,
|
22 |
+
top_p=0.9
|
23 |
+
)
|
24 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Assistant:")[-1].strip()
|
25 |
+
|
26 |
+
# Update history
|
27 |
+
history.append((input_text, response))
|
28 |
+
return response, history
|
29 |
+
|
30 |
+
# Gradio Interface
|
31 |
+
with gr.Blocks() as interface:
|
32 |
+
gr.Markdown("### Mistral-7B-v0.2 Chatbot")
|
33 |
+
gr.Markdown("This chatbot is powered by the Mistral-7B-v0.2 model for summarization and general conversation.")
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
chatbot_box = gr.Chatbot(label="Chatbot")
|
37 |
+
|
38 |
+
with gr.Row():
|
39 |
+
user_input = gr.Textbox(label="Your Input", placeholder="Type your message here...")
|
40 |
+
submit_button = gr.Button("Submit")
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
reset_button = gr.Button("Reset Chat")
|
44 |
+
|
45 |
+
# Functionality
|
46 |
+
submit_button.click(chatbot, inputs=[user_input, chatbot_box], outputs=[chatbot_box, chatbot_box])
|
47 |
+
reset_button.click(lambda: [], inputs=[], outputs=[chatbot_box])
|
48 |
+
|
49 |
+
# Launch the app
|
50 |
+
interface.launch()
|