Spaces:
Sleeping
Sleeping
enotkrutoy
commited on
Commit
•
6b07d33
1
Parent(s):
2bca226
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
|
4 |
-
# Load model
|
5 |
tokenizer = AutoTokenizer.from_pretrained("Leo022/Gemma_QA_For_Telegram_Bot")
|
6 |
model = AutoModelForCausalLM.from_pretrained("Leo022/Gemma_QA_For_Telegram_Bot")
|
7 |
|
8 |
-
def respond(
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
):
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
messages = [{"role": "system", "content": system_message}]
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
23 |
|
|
|
24 |
messages.append({"role": "user", "content": message})
|
25 |
|
26 |
-
|
|
|
27 |
|
|
|
28 |
output = model.generate(
|
29 |
input_ids,
|
30 |
max_length=max_tokens,
|
@@ -33,25 +43,22 @@ def respond(
|
|
33 |
do_sample=True,
|
34 |
)
|
35 |
|
|
|
36 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
37 |
|
38 |
return response
|
39 |
|
|
|
40 |
demo = gr.ChatInterface(
|
41 |
respond,
|
42 |
additional_inputs=[
|
43 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
44 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
45 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
46 |
-
gr.Slider(
|
47 |
-
minimum=0.1,
|
48 |
-
maximum=1.0,
|
49 |
-
value=0.95,
|
50 |
-
step=0.05,
|
51 |
-
label="Top-p (nucleus sampling)",
|
52 |
-
),
|
53 |
],
|
54 |
)
|
55 |
|
56 |
if __name__ == "__main__":
|
|
|
57 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
|
4 |
+
# Load model and tokenizer
|
5 |
tokenizer = AutoTokenizer.from_pretrained("Leo022/Gemma_QA_For_Telegram_Bot")
|
6 |
model = AutoModelForCausalLM.from_pretrained("Leo022/Gemma_QA_For_Telegram_Bot")
|
7 |
|
8 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
9 |
+
"""
|
10 |
+
Function to generate response from the model.
|
11 |
+
Args:
|
12 |
+
message (str): The user's input message.
|
13 |
+
history (list): The conversation history.
|
14 |
+
system_message (str): The system message.
|
15 |
+
max_tokens (int): Maximum number of tokens for output.
|
16 |
+
temperature (float): Sampling temperature.
|
17 |
+
top_p (float): Nucleus sampling parameter.
|
18 |
+
Returns:
|
19 |
+
str: The model's response.
|
20 |
+
"""
|
21 |
+
# Initialize messages list with the system message
|
22 |
messages = [{"role": "system", "content": system_message}]
|
23 |
|
24 |
+
# Add conversation history to messages
|
25 |
+
for user_msg, assistant_msg in history:
|
26 |
+
if user_msg:
|
27 |
+
messages.append({"role": "user", "content": user_msg})
|
28 |
+
if assistant_msg:
|
29 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
30 |
|
31 |
+
# Append the latest user message
|
32 |
messages.append({"role": "user", "content": message})
|
33 |
|
34 |
+
# Encode the concatenation of all message contents
|
35 |
+
input_ids = tokenizer.encode(" ".join([msg["content"] for msg in messages]), return_tensors="pt")
|
36 |
|
37 |
+
# Generate response
|
38 |
output = model.generate(
|
39 |
input_ids,
|
40 |
max_length=max_tokens,
|
|
|
43 |
do_sample=True,
|
44 |
)
|
45 |
|
46 |
+
# Decode the generated tokens to get the response text
|
47 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
48 |
|
49 |
return response
|
50 |
|
51 |
+
# Define the Gradio interface
|
52 |
demo = gr.ChatInterface(
|
53 |
respond,
|
54 |
additional_inputs=[
|
55 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
56 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
57 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
58 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
],
|
60 |
)
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
+
# Launch the Gradio app
|
64 |
demo.launch()
|