Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -15,50 +23,68 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a
|
50 |
-
gr.Slider(minimum=1, maximum=
|
51 |
-
gr.Slider(minimum=0.1, maximum=
|
52 |
gr.Slider(
|
53 |
minimum=0.1,
|
54 |
maximum=1.0,
|
55 |
-
value=0.
|
56 |
step=0.05,
|
57 |
label="Top-p (nucleus sampling)",
|
58 |
),
|
59 |
],
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load your fine-tuned model
|
6 |
+
model_id = "ragunath-ravi/distilgpt2-lmsys-chat"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
9 |
|
10 |
+
# Set padding token to be the same as EOS token if not set
|
11 |
+
if tokenizer.pad_token is None:
|
12 |
+
tokenizer.pad_token = tokenizer.eos_token
|
13 |
+
|
14 |
+
# Move model to GPU if available
|
15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
+
model = model.to(device)
|
17 |
|
18 |
def respond(
|
19 |
message,
|
|
|
23 |
temperature,
|
24 |
top_p,
|
25 |
):
|
26 |
+
# Format the conversation history as expected by the model
|
27 |
+
prompt = system_message + "\n\n"
|
28 |
+
|
29 |
+
for user_msg, assistant_msg in history:
|
30 |
+
if user_msg:
|
31 |
+
prompt += f"User: {user_msg}\n"
|
32 |
+
if assistant_msg:
|
33 |
+
prompt += f"Assistant: {assistant_msg}\n"
|
34 |
+
|
35 |
+
# Add the latest user message
|
36 |
+
prompt += f"User: {message}\nAssistant:"
|
37 |
+
|
38 |
+
# Tokenize the prompt
|
39 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
40 |
+
|
41 |
+
# Generate response
|
42 |
+
with torch.no_grad():
|
43 |
+
output = model.generate(
|
44 |
+
inputs["input_ids"],
|
45 |
+
max_new_tokens=max_tokens,
|
46 |
+
temperature=temperature,
|
47 |
+
top_p=top_p,
|
48 |
+
do_sample=True,
|
49 |
+
pad_token_id=tokenizer.eos_token_id,
|
50 |
+
attention_mask=inputs["attention_mask"],
|
51 |
+
)
|
52 |
+
|
53 |
+
# Decode the generated response
|
54 |
+
full_response = tokenizer.decode(output[0], skip_special_tokens=True)
|
55 |
+
|
56 |
+
# Extract only the assistant's part from the full response
|
57 |
+
assistant_response = full_response[len(prompt):].strip()
|
58 |
+
|
59 |
+
# Sometimes the model might continue with "User:" - we need to cut that off
|
60 |
+
if "User:" in assistant_response:
|
61 |
+
assistant_response = assistant_response.split("User:")[0].strip()
|
62 |
+
|
63 |
+
# Stream the response token by token (simulated for this model)
|
64 |
+
response_so_far = ""
|
65 |
+
tokens = assistant_response.split()
|
66 |
+
for token in tokens:
|
67 |
+
response_so_far += token + " "
|
68 |
+
yield response_so_far.strip()
|
69 |
|
70 |
+
# Create the Gradio chat interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
demo = gr.ChatInterface(
|
72 |
respond,
|
73 |
additional_inputs=[
|
74 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
75 |
+
gr.Slider(minimum=1, maximum=512, value=128, step=1, label="Max new tokens"),
|
76 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
77 |
gr.Slider(
|
78 |
minimum=0.1,
|
79 |
maximum=1.0,
|
80 |
+
value=0.9,
|
81 |
step=0.05,
|
82 |
label="Top-p (nucleus sampling)",
|
83 |
),
|
84 |
],
|
85 |
+
title="DistilGPT-2 Chat Assistant",
|
86 |
+
description="A simple chatbot powered by a fine-tuned DistilGPT-2 model on the LMSYS Chat 1M dataset.",
|
87 |
)
|
88 |
|
|
|
89 |
if __name__ == "__main__":
|
90 |
+
demo.launch()
|