Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,59 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
)
|
9 |
|
10 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
11 |
-
# Format the
|
12 |
-
|
13 |
|
14 |
-
# Add
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# Add conversation history
|
19 |
-
for msg in history:
|
20 |
-
if isinstance(msg, list) and len(msg) == 2:
|
21 |
-
prompt += f"User: {msg[0]}\nAssistant: {msg[1]}\n"
|
22 |
|
23 |
# Add current message
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
for token in client.text_generation(
|
38 |
-
prompt,
|
39 |
-
stream=True,
|
40 |
-
**parameters
|
41 |
-
):
|
42 |
-
response += token
|
43 |
-
yield response
|
44 |
-
except Exception as e:
|
45 |
-
yield f"Error: {str(e)}"
|
46 |
|
47 |
# Create the interface
|
48 |
demo = gr.ChatInterface(
|
49 |
respond,
|
50 |
additional_inputs=[
|
51 |
gr.Textbox(
|
52 |
-
value="You are a
|
53 |
label="System message"
|
54 |
),
|
55 |
gr.Slider(
|
@@ -73,7 +77,9 @@ demo = gr.ChatInterface(
|
|
73 |
step=0.05,
|
74 |
label="Top-p (nucleus sampling)"
|
75 |
),
|
76 |
-
]
|
|
|
|
|
77 |
)
|
78 |
|
79 |
if __name__ == "__main__":
|
|
|
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__":
|