ajeetkumar01
commited on
Commit
•
57f5d5d
1
Parent(s):
c4a1e99
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
# Load pre-trained model and tokenizer
|
5 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
+
|
9 |
+
def generate_response(messages):
|
10 |
+
"""
|
11 |
+
Generate response based on the given user messages.
|
12 |
+
Parameters:
|
13 |
+
- messages (list): A list of dictionaries containing user messages with roles.
|
14 |
+
Returns:
|
15 |
+
- response (str): The generated response.
|
16 |
+
"""
|
17 |
+
# Apply chat template and encode messages
|
18 |
+
encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")
|
19 |
+
# Move inputs to device
|
20 |
+
model_inputs = encodeds.to("cuda") # Assuming CUDA device is available
|
21 |
+
# Generate response
|
22 |
+
generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
|
23 |
+
# Decode the generated response
|
24 |
+
response = tokenizer.batch_decode(generated_ids)[0]
|
25 |
+
return response
|
26 |
+
|
27 |
+
# Define Gradio interface components
|
28 |
+
input_chat = gr.Textbox(lines=5, label="Input Chat", placeholder="Enter chat messages...")
|
29 |
+
output_response = gr.Textbox(label="Generated Response", placeholder="Generated response will appear here...")
|
30 |
+
|
31 |
+
# Create Gradio interface
|
32 |
+
gr.Interface(generate_response, input_chat, output_response,
|
33 |
+
title="Chat Response Generation",
|
34 |
+
description="Generate responses based on user messages using Mistral AI model.",
|
35 |
+
theme="default",
|
36 |
+
allow_flagging="never").launch()
|