Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,22 +2,41 @@ import gradio as gr
|
|
2 |
import spaces
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
return
|
13 |
-
max_new_tokens=256,
|
14 |
-
temperature=0.7,
|
15 |
-
do_sample=True)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
)
|
23 |
demo.launch()
|
|
|
2 |
import spaces
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Initialize model once at startup
|
6 |
+
model = pipeline(
|
7 |
+
"text-generation",
|
8 |
+
model="unsloth/DeepSeek-R1-Distill-Llama-8B",
|
9 |
+
torch_dtype="auto",
|
10 |
+
device_map="auto"
|
11 |
+
)
|
12 |
+
|
13 |
+
@spaces.GPU(duration=120)
|
14 |
+
def chat_response(message, history):
|
15 |
+
# Format conversation history for model input
|
16 |
+
messages = []
|
17 |
+
for human, assistant in history:
|
18 |
+
messages.extend([
|
19 |
+
{"role": "user", "content": human},
|
20 |
+
{"role": "assistant", "content": assistant}
|
21 |
+
])
|
22 |
+
messages.append({"role": "user", "content": message})
|
23 |
+
|
24 |
+
# Generate response
|
25 |
+
response = model(
|
26 |
+
messages,
|
27 |
+
max_new_tokens=256,
|
28 |
+
temperature=0.7,
|
29 |
+
do_sample=True
|
30 |
+
)
|
31 |
|
32 |
+
return response[0]['generated_text'][-1]["content"]
|
|
|
|
|
|
|
33 |
|
34 |
+
# Create chat interface
|
35 |
+
demo = gr.ChatInterface(
|
36 |
+
chat_response,
|
37 |
+
chatbot=gr.Chatbot(height=500),
|
38 |
+
textbox=gr.Textbox(placeholder="Ask me anything...", container=False, scale=7),
|
39 |
+
title="DeepSeek-Llama-8B Chat Demo",
|
40 |
+
examples=[["Explain quantum computing simply"], ["Write a Python function for Fibonacci sequence"]]
|
41 |
)
|
42 |
demo.launch()
|