ruslanmv commited on
Commit
fc32600
·
verified ·
1 Parent(s): 2b8a359

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -44
app.py CHANGED
@@ -4,10 +4,9 @@ from functools import lru_cache
4
  # Cache model loading to optimize performance
5
  @lru_cache(maxsize=3)
6
  def load_hf_model(model_name):
7
- # Use the Gradio-built huggingface loader instead of transformers_gradio
8
  return gr.load(
9
  name=f"deepseek-ai/{model_name}",
10
- src="huggingface", # Changed from transformers_gradio.registry
11
  api_name="/chat"
12
  )
13
 
@@ -18,56 +17,57 @@ MODELS = {
18
  "DeepSeek-R1-Zero": load_hf_model("DeepSeek-R1-Zero")
19
  }
20
 
21
- # --- Chatbot function ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
23
  history = history or []
24
 
25
- # Get the selected model component
26
  model_component = MODELS[model_choice]
27
 
28
- # Create payload for the model
 
 
 
 
 
29
  payload = {
30
- "messages": [{"role": "user", "content": input_text}],
31
- "system": system_message,
32
  "max_tokens": max_new_tokens,
33
  "temperature": temperature,
34
  "top_p": top_p
35
  }
36
 
37
- # Run inference using the selected model
38
  try:
39
- response = model_component(payload) # The response is likely a dictionary
40
- if isinstance(response, dict) and "choices" in response:
41
- # Assuming the response structure is similar to OpenAI's API
42
- assistant_response = response["choices"][0]["message"]["content"]
43
- elif isinstance(response, dict) and "generated_text" in response:
44
- # If the response is in a different format, adjust accordingly
45
- assistant_response = response["generated_text"]
46
- else:
47
- assistant_response = "Unexpected model response format."
48
  except Exception as e:
49
  assistant_response = f"Error: {str(e)}"
50
 
51
- # Append user and assistant messages to history
52
  history.append((input_text, assistant_response))
53
-
54
  return history, history, ""
55
 
56
- # --- Gradio Interface ---
57
  with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as demo:
58
- gr.Markdown(
59
- """
60
- # DeepSeek Chatbot
61
- Created by [ruslanmv.com](https://ruslanmv.com/)
62
- This is a demo of different DeepSeek models. Select a model, type your message, and click "Submit".
63
- You can also adjust optional parameters like system message, max new tokens, temperature, and top-p.
64
- """
65
- )
66
 
67
  with gr.Row():
68
  with gr.Column():
69
- chatbot_output = gr.Chatbot(label="DeepSeek Chatbot", height=500)
70
- msg = gr.Textbox(label="Your Message", placeholder="Type your message here...")
71
  with gr.Row():
72
  submit_btn = gr.Button("Submit", variant="primary")
73
  clear_btn = gr.ClearButton([msg, chatbot_output])
@@ -76,28 +76,19 @@ with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as demo:
76
  with gr.Accordion("Options", open=True):
77
  model_choice = gr.Radio(
78
  choices=list(MODELS.keys()),
79
- label="Choose a Model",
80
  value="DeepSeek-R1"
81
  )
82
  with gr.Accordion("Optional Parameters", open=False):
83
  system_message = gr.Textbox(
84
- label="System Message",
85
- value="You are a friendly Chatbot created by ruslanmv.com",
86
- lines=2,
87
- )
88
- max_new_tokens = gr.Slider(
89
- minimum=1, maximum=4000, value=200, label="Max New Tokens"
90
- )
91
- temperature = gr.Slider(
92
- minimum=0.10, maximum=4.00, value=0.70, label="Temperature"
93
- )
94
- top_p = gr.Slider(
95
- minimum=0.10, maximum=1.00, value=0.90, label="Top-p (nucleus sampling)"
96
  )
 
 
 
97
 
98
  chat_history = gr.State([])
99
 
100
- # Event handling
101
  submit_btn.click(
102
  chatbot,
103
  [msg, chat_history, model_choice, system_message, max_new_tokens, temperature, top_p],
 
4
  # Cache model loading to optimize performance
5
  @lru_cache(maxsize=3)
6
  def load_hf_model(model_name):
 
7
  return gr.load(
8
  name=f"deepseek-ai/{model_name}",
9
+ src="huggingface",
10
  api_name="/chat"
11
  )
12
 
 
17
  "DeepSeek-R1-Zero": load_hf_model("DeepSeek-R1-Zero")
18
  }
19
 
20
+ def parse_response(response):
21
+ """Handle different response formats from various models"""
22
+ if isinstance(response, list):
23
+ if len(response) > 0:
24
+ # Handle list of messages format
25
+ return response[0].get('generated_text',
26
+ response[0].get('content', str(response[0])))
27
+ elif isinstance(response, dict):
28
+ # Handle OpenAI-style format
29
+ if 'choices' in response:
30
+ return response['choices'][0]['message']['content']
31
+ # Handle standard text generation format
32
+ elif 'generated_text' in response:
33
+ return response['generated_text']
34
+ return f"Unsupported response format: {type(response)}"
35
+
36
  def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
37
  history = history or []
38
 
 
39
  model_component = MODELS[model_choice]
40
 
41
+ # Construct messages with optional system message
42
+ messages = []
43
+ if system_message.strip():
44
+ messages.append({"role": "system", "content": system_message})
45
+ messages.append({"role": "user", "content": input_text})
46
+
47
  payload = {
48
+ "messages": messages,
 
49
  "max_tokens": max_new_tokens,
50
  "temperature": temperature,
51
  "top_p": top_p
52
  }
53
 
 
54
  try:
55
+ response = model_component(payload)
56
+ assistant_response = parse_response(response)
 
 
 
 
 
 
 
57
  except Exception as e:
58
  assistant_response = f"Error: {str(e)}"
59
 
 
60
  history.append((input_text, assistant_response))
 
61
  return history, history, ""
62
 
63
+ # Interface setup remains the same
64
  with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as demo:
65
+ gr.Markdown("""# DeepSeek Chatbot""")
 
 
 
 
 
 
 
66
 
67
  with gr.Row():
68
  with gr.Column():
69
+ chatbot_output = gr.Chatbot(height=500)
70
+ msg = gr.Textbox(placeholder="Type your message...")
71
  with gr.Row():
72
  submit_btn = gr.Button("Submit", variant="primary")
73
  clear_btn = gr.ClearButton([msg, chatbot_output])
 
76
  with gr.Accordion("Options", open=True):
77
  model_choice = gr.Radio(
78
  choices=list(MODELS.keys()),
 
79
  value="DeepSeek-R1"
80
  )
81
  with gr.Accordion("Optional Parameters", open=False):
82
  system_message = gr.Textbox(
83
+ value="You are a helpful AI assistant",
84
+ lines=2
 
 
 
 
 
 
 
 
 
 
85
  )
86
+ max_new_tokens = gr.Slider(1, 4000, 200)
87
+ temperature = gr.Slider(0.1, 4.0, 0.7)
88
+ top_p = gr.Slider(0.1, 1.0, 0.9)
89
 
90
  chat_history = gr.State([])
91
 
 
92
  submit_btn.click(
93
  chatbot,
94
  [msg, chat_history, model_choice, system_message, max_new_tokens, temperature, top_p],