ruslanmv commited on
Commit
390d1d1
·
verified ·
1 Parent(s): ba773b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -31
app.py CHANGED
@@ -1,42 +1,24 @@
1
  import gradio as gr
2
- from functools import lru_cache
3
-
4
- # Cache model loading to optimize performance
5
- @lru_cache(maxsize=3)
6
- def load_hf_model(model_name):
7
- return gr.load(f"models/{model_name}", src="huggingface")
8
-
9
- # Load all models at startup
10
- MODELS = {
11
- "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B": load_hf_model("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"),
12
- "deepseek-ai/DeepSeek-R1": load_hf_model("deepseek-ai/DeepSeek-R1"),
13
- "deepseek-ai/DeepSeek-R1-Zero": load_hf_model("deepseek-ai/DeepSeek-R1-Zero")
14
- }
15
 
16
  # --- Chatbot function ---
17
  def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
18
  history = history or []
19
 
20
- # Get the selected model component
21
- model_component = MODELS[model_choice]
22
-
23
  # Create payload for the model
24
  payload = {
25
- "inputs": input_text, # Directly pass the input text
26
- "parameters": {
27
- "max_new_tokens": max_new_tokens,
28
- "temperature": temperature,
29
- "top_p": top_p,
30
- "return_full_text": False # Only return the generated text
31
- }
32
  }
33
 
34
  # Run inference using the selected model
35
  try:
36
- response = model_component(**payload) # Pass payload as keyword arguments
37
- if isinstance(response, list) and len(response) > 0:
38
- # Extract the generated text from the response
39
- assistant_response = response[0].get("generated_text", "No response generated.")
40
  else:
41
  assistant_response = "Unexpected model response format."
42
  except Exception as e:
@@ -48,7 +30,7 @@ def chatbot(input_text, history, model_choice, system_message, max_new_tokens, t
48
  return history, history, ""
49
 
50
  # --- Gradio Interface ---
51
- with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as demo:
52
  gr.Markdown(
53
  """
54
  # DeepSeek Chatbot
@@ -69,9 +51,9 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as demo:
69
  with gr.Row():
70
  with gr.Accordion("Options", open=True):
71
  model_choice = gr.Radio(
72
- choices=list(MODELS.keys()),
73
  label="Choose a Model",
74
- value="deepseek-ai/DeepSeek-R1"
75
  )
76
  with gr.Accordion("Optional Parameters", open=False):
77
  system_message = gr.Textbox(
@@ -104,4 +86,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as demo:
104
  )
105
 
106
  if __name__ == "__main__":
107
- demo.launch()
 
1
  import gradio as gr
2
+ from models import demo # Import the demo object from models.py
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  # --- Chatbot function ---
5
  def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
6
  history = history or []
7
 
 
 
 
8
  # Create payload for the model
9
  payload = {
10
+ "messages": [{"role": "user", "content": input_text}],
11
+ "system": system_message,
12
+ "max_tokens": max_new_tokens,
13
+ "temperature": temperature,
14
+ "top_p": top_p
 
 
15
  }
16
 
17
  # Run inference using the selected model
18
  try:
19
+ response = demo(payload) # Use the demo object directly
20
+ if isinstance(response, dict) and "choices" in response:
21
+ assistant_response = response["choices"][0]["message"]["content"]
 
22
  else:
23
  assistant_response = "Unexpected model response format."
24
  except Exception as e:
 
30
  return history, history, ""
31
 
32
  # --- Gradio Interface ---
33
+ with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as app:
34
  gr.Markdown(
35
  """
36
  # DeepSeek Chatbot
 
51
  with gr.Row():
52
  with gr.Accordion("Options", open=True):
53
  model_choice = gr.Radio(
54
+ choices=["DeepSeek-R1-Distill-Qwen-32B", "DeepSeek-R1", "DeepSeek-R1-Zero"],
55
  label="Choose a Model",
56
+ value="DeepSeek-R1"
57
  )
58
  with gr.Accordion("Optional Parameters", open=False):
59
  system_message = gr.Textbox(
 
86
  )
87
 
88
  if __name__ == "__main__":
89
+ app.launch()