Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,59 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
def respond(message, history
|
8 |
-
# Format the
|
9 |
-
|
10 |
-
|
11 |
-
# Add system message if provided
|
12 |
-
if system_message:
|
13 |
-
prompt = f"{system_message}\n"
|
14 |
|
15 |
-
# Add
|
16 |
for user_msg, assistant_msg in history:
|
17 |
-
|
|
|
18 |
|
19 |
# Add current message
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
prompt,
|
26 |
-
max_new_tokens=max_tokens,
|
27 |
-
stream=True,
|
28 |
-
temperature=temperature,
|
29 |
-
top_p=top_p,
|
30 |
-
):
|
31 |
-
response += token
|
32 |
-
yield response
|
33 |
|
34 |
# Create the interface
|
35 |
demo = gr.ChatInterface(
|
36 |
respond,
|
37 |
additional_inputs=[
|
38 |
gr.Textbox(
|
39 |
-
value="You are a
|
40 |
label="System message"
|
41 |
),
|
42 |
gr.Slider(
|
@@ -60,8 +77,10 @@ demo = gr.ChatInterface(
|
|
60 |
step=0.05,
|
61 |
label="Top-p (nucleus sampling)"
|
62 |
),
|
63 |
-
]
|
|
|
|
|
64 |
)
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load model and tokenizer
|
6 |
+
model_name = "davnas/Italian_Cousine_2.1"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
model_name,
|
10 |
+
torch_dtype=torch.float32, # Use float32 for CPU
|
11 |
+
low_cpu_mem_usage=True,
|
12 |
+
device_map="auto"
|
13 |
+
)
|
14 |
|
15 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
16 |
+
# Format the conversation
|
17 |
+
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Add history
|
20 |
for user_msg, assistant_msg in history:
|
21 |
+
messages.append({"role": "user", "content": user_msg})
|
22 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
23 |
|
24 |
# Add current message
|
25 |
+
messages.append({"role": "user", "content": message})
|
26 |
+
|
27 |
+
# Create the prompt using the tokenizer's chat template
|
28 |
+
input_ids = tokenizer.apply_chat_template(
|
29 |
+
messages,
|
30 |
+
tokenize=True,
|
31 |
+
add_generation_prompt=True,
|
32 |
+
return_tensors="pt"
|
33 |
+
)
|
34 |
+
|
35 |
+
# Generate response
|
36 |
+
with torch.no_grad():
|
37 |
+
output_ids = model.generate(
|
38 |
+
input_ids,
|
39 |
+
max_new_tokens=max_tokens,
|
40 |
+
do_sample=True,
|
41 |
+
temperature=temperature,
|
42 |
+
top_p=top_p,
|
43 |
+
pad_token_id=tokenizer.pad_token_id,
|
44 |
+
streaming=True
|
45 |
+
)
|
46 |
|
47 |
+
# Decode and return the response
|
48 |
+
response = tokenizer.decode(output_ids[0][len(input_ids[0]):], skip_special_tokens=True)
|
49 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Create the interface
|
52 |
demo = gr.ChatInterface(
|
53 |
respond,
|
54 |
additional_inputs=[
|
55 |
gr.Textbox(
|
56 |
+
value="You are a professional chef assistant who provides accurate and detailed recipes.",
|
57 |
label="System message"
|
58 |
),
|
59 |
gr.Slider(
|
|
|
77 |
step=0.05,
|
78 |
label="Top-p (nucleus sampling)"
|
79 |
),
|
80 |
+
],
|
81 |
+
title="Italian Cuisine Chatbot",
|
82 |
+
description="Ask me anything about Italian cuisine or cooking!"
|
83 |
)
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|