legaltextai commited on
Commit
d46b7dc
·
verified ·
1 Parent(s): c0eecae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -2,22 +2,41 @@ import gradio as gr
2
  import spaces
3
  from transformers import pipeline
4
 
5
- @spaces.GPU(duration=120) # Increased timeout for LLM inference
6
- def generate_response(messages):
7
- pipe = pipeline("text-generation",
8
- model="unsloth/DeepSeek-R1-Distill-Llama-8B",
9
- torch_dtype="auto",
10
- device_map="auto")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- return pipe(messages,
13
- max_new_tokens=256,
14
- temperature=0.7,
15
- do_sample=True)
16
 
17
- demo = gr.Interface(
18
- fn=generate_response,
19
- inputs=gr.JSON(value=[{"role": "user", "content": "Who are you?"}]),
20
- outputs=gr.Text(),
21
- title="DeepSeek-Llama-8B ZeroGPU Demo"
 
 
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()