ruslanmv commited on
Commit
d9faa8c
·
verified ·
1 Parent(s): 9122113

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -52
app.py CHANGED
@@ -1,60 +1,52 @@
1
  import gradio as gr
2
- import subprocess
 
 
3
 
4
- # Function to load a model using Hugging Face Spaces
5
- def load_model_from_space(model_name):
6
- print(f"Attempting to load {model_name}...")
7
- try:
8
- demo = gr.load(name=model_name, src="spaces")
9
- print(f"Successfully loaded {model_name}")
10
- return demo
11
- except Exception as e:
12
- print(f"Error loading model {model_name}: {e}")
13
- return None
14
 
15
- # Load the models
16
- deepseek_r1_distill = load_model_from_space("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B")
17
- deepseek_r1 = load_model_from_space("deepseek-ai/DeepSeek-R1")
18
- deepseek_r1_zero = load_model_from_space("deepseek-ai/DeepSeek-R1-Zero")
 
 
19
 
20
  # --- Chatbot function ---
21
  def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
22
  history = history or []
23
- print(f"Input: {input_text}, History: {history}, Model: {model_choice}")
24
-
25
- # Choose the model based on user selection
26
- if model_choice == "DeepSeek-R1-Distill-Qwen-32B" and deepseek_r1_distill:
27
- model_demo = deepseek_r1_distill
28
- elif model_choice == "DeepSeek-R1" and deepseek_r1:
29
- model_demo = deepseek_r1
30
- elif model_choice == "DeepSeek-R1-Zero" and deepseek_r1_zero:
31
- model_demo = deepseek_r1_zero
32
- else:
33
- default_response = "Model not selected or could not be loaded."
34
- history.append((input_text, default_response))
35
- return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
36
-
37
- # Call the model's 'predict' function.
38
  try:
39
- model_output = model_demo(input_text, history, max_new_tokens, temperature, top_p, system_message, fn_index=0)
 
40
  except Exception as e:
41
- print(f"An error occurred: {e}")
42
- model_output= "An error occurred please check the model and try again."
43
- history.append((input_text, model_output))
44
- return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
45
-
46
- # Check if model_output is iterable and has expected number of elements
47
- if not isinstance(model_output, (list, tuple)) or len(model_output) < 2:
48
- error_message = "Model output does not have the expected format."
49
- history.append((input_text, error_message))
50
- return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
51
-
52
- response = model_output[-1][1] if model_output[-1][1] else "Model did not return a response."
53
- history.append((input_text, response))
54
- return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
55
 
56
  # --- Gradio Interface ---
57
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
58
  gr.Markdown(
59
  """
60
  # DeepSeek Chatbot
@@ -73,11 +65,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
73
  submit_btn = gr.Button("Submit", variant="primary")
74
  clear_btn = gr.ClearButton([msg, chatbot_output])
75
 
76
- # Options moved below the chat interface
77
  with gr.Row():
78
  with gr.Accordion("Options", open=True):
79
  model_choice = gr.Radio(
80
- choices=["DeepSeek-R1-Distill-Qwen-32B", "DeepSeek-R1", "DeepSeek-R1-Zero"],
81
  label="Choose a Model",
82
  value="DeepSeek-R1"
83
  )
@@ -97,21 +88,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
97
  minimum=0.10, maximum=1.00, value=0.90, label="Top-p (nucleus sampling)"
98
  )
99
 
100
- # Maintain chat history
101
  chat_history = gr.State([])
102
 
103
  # Event handling
104
  submit_btn.click(
105
  chatbot,
106
  [msg, chat_history, model_choice, system_message, max_new_tokens, temperature, top_p],
107
- [chatbot_output, chat_history, msg, model_choice, system_message, max_new_tokens, temperature, top_p],
108
  )
109
  msg.submit(
110
  chatbot,
111
  [msg, chat_history, model_choice, system_message, max_new_tokens, temperature, top_p],
112
- [chatbot_output, chat_history, msg, model_choice, system_message, max_new_tokens, temperature, top_p],
113
  )
114
 
115
- # Launch the demo
 
 
 
 
116
  if __name__ == "__main__":
117
  demo.launch()
 
1
  import gradio as gr
2
+ import spaces
3
+ import transformers_gradio
4
+ from functools import lru_cache
5
 
6
+ # Cache model loading to optimize performance
7
+ @lru_cache(maxsize=3)
8
+ def load_hf_model(model_name):
9
+ return gr.load(
10
+ name=f"deepseek-ai/{model_name}",
11
+ src=transformers_gradio.registry,
12
+ api_name="/chat"
13
+ )
 
 
14
 
15
+ # Load all models at startup
16
+ MODELS = {
17
+ "DeepSeek-R1-Distill-Qwen-32B": load_hf_model("DeepSeek-R1-Distill-Qwen-32B"),
18
+ "DeepSeek-R1": load_hf_model("DeepSeek-R1"),
19
+ "DeepSeek-R1-Zero": load_hf_model("DeepSeek-R1-Zero")
20
+ }
21
 
22
  # --- Chatbot function ---
23
  def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
24
  history = history or []
25
+
26
+ # Get the selected model component
27
+ model_component = MODELS[model_choice]
28
+
29
+ # Create payload for the model
30
+ payload = {
31
+ "messages": [{"role": "user", "content": input_text}],
32
+ "system": system_message,
33
+ "max_tokens": max_new_tokens,
34
+ "temperature": temperature,
35
+ "top_p": top_p
36
+ }
37
+
38
+ # Run inference using the selected model
 
39
  try:
40
+ response = model_component(payload)
41
+ assistant_response = response[-1]["content"]
42
  except Exception as e:
43
+ assistant_response = f"Error: {str(e)}"
44
+
45
+ history.append((input_text, assistant_response))
46
+ return history, history, ""
 
 
 
 
 
 
 
 
 
 
47
 
48
  # --- Gradio Interface ---
49
+ with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as demo:
50
  gr.Markdown(
51
  """
52
  # DeepSeek Chatbot
 
65
  submit_btn = gr.Button("Submit", variant="primary")
66
  clear_btn = gr.ClearButton([msg, chatbot_output])
67
 
 
68
  with gr.Row():
69
  with gr.Accordion("Options", open=True):
70
  model_choice = gr.Radio(
71
+ choices=list(MODELS.keys()),
72
  label="Choose a Model",
73
  value="DeepSeek-R1"
74
  )
 
88
  minimum=0.10, maximum=1.00, value=0.90, label="Top-p (nucleus sampling)"
89
  )
90
 
 
91
  chat_history = gr.State([])
92
 
93
  # Event handling
94
  submit_btn.click(
95
  chatbot,
96
  [msg, chat_history, model_choice, system_message, max_new_tokens, temperature, top_p],
97
+ [chatbot_output, chat_history, msg]
98
  )
99
  msg.submit(
100
  chatbot,
101
  [msg, chat_history, model_choice, system_message, max_new_tokens, temperature, top_p],
102
+ [chatbot_output, chat_history, msg]
103
  )
104
 
105
+ # Add GPU support for Hugging Face Spaces
106
+ demo.fn = spaces.GPU()(demo.fn)
107
+ for fn in demo.fns.values():
108
+ fn.api_name = False
109
+
110
  if __name__ == "__main__":
111
  demo.launch()