Update app.py
Browse files3 unmodified lines4567781091011124 unmodified lines171819212021222324252627282930313233343536373825394028414243444546473031484950515253375439404142434445464755565758595160536162566364585960616263646565666768697069707172732 unmodified lines76777879798081828485868384878889909192939495858687888990911009293943 unmodified lines# Cache model loading to optimize performance@lru_cache(maxsize=3)def load_hf_model(model_name): # Use the Gradio-built huggingface loader instead of transformers_gradio return gr.load( name=f"deepseek-ai/{model_name}", src="huggingface", # Changed from transformers_gradio.registry src="huggingface", api_name="/chat" )
4 unmodified lines "DeepSeek-R1-Zero": load_hf_model("DeepSeek-R1-Zero")}
# --- Chatbot function ---def parse_response(response): """Handle different response formats from various models""" if isinstance(response, list): if len(response) > 0: # Handle list of messages format return response[0].get('generated_text', response[0].get('content', str(response[0]))) elif isinstance(response, dict): # Handle OpenAI-style format if 'choices' in response: return response['choices'][0]['message']['content'] # Handle standard text generation format elif 'generated_text' in response: return response['generated_text'] return f"Unsupported response format: {type(response)}"
def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p): history = history or [] # Get the selected model component model_component = MODELS[model_choice] # Create payload for the model # Construct messages with optional system message messages = [] if system_message.strip(): messages.append({"role": "system", "content": system_message}) messages.append({"role": "user", "content": input_text}) payload = { "messages": [{"role": "user", "content": input_text}], "system": system_message, "messages": messages, "max_tokens": max_new_tokens, "temperature": temperature, "top_p": top_p } # Run inference using the selected model try: response = model_component(payload) # The response is likely a dictionary if isinstance(response, dict) and "choices" in response: # Assuming the response structure is similar to OpenAI's API assistant_response = response["choices"][0]["message"]["content"] elif isinstance(response, dict) and "generated_text" in response: # If the response is in a different format, adjust accordingly assistant_response = response["generated_text"] else: assistant_response = "Unexpected model response format." response = model_component(payload) assistant_response = parse_response(response) except Exception as e: assistant_response = f"Error: {str(e)}" # Append user and assistant messages to history history.append((input_text, assistant_response)) return history, history, ""
# --- Gradio Interface ---# Interface setup remains the samewith gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as demo: gr.Markdown( """ # DeepSeek Chatbot Created by [ruslanmv.com](https://ruslanmv.com/) This is a demo of different DeepSeek models. Select a model, type your message, and click "Submit". You can also adjust optional parameters like system message, max new tokens, temperature, and top-p. """ ) gr.Markdown("""# DeepSeek Chatbot""")
with gr.Row(): with gr.Column(): chatbot_output = gr.Chatbot(label="DeepSeek Chatbot", height=500) msg = gr.Textbox(label="Your Message", placeholder="Type your message here...") chatbot_output = gr.Chatbot(height=500) msg = gr.Textbox(placeholder="Type your message...") with gr.Row(): submit_btn = gr.Button("Submit", variant="primary") clear_btn = gr.ClearButton([msg, chatbot_output])2 unmodified lines with gr.Accordion("Options", open=True): model_choice = gr.Radio( choices=list(MODELS.keys()), label="Choose a Model", value="DeepSeek-R1" ) with gr.Accordion("Optional Parameters", open=False): system_message = gr.Textbox( label="System Message", value="You are a friendly Chatbot created by ruslanmv.com", lines=2, value="You are a helpful AI assistant", lines=2 ) max_new_tokens = gr.Slider( minimum=1, maximum=4000, value=200, label="Max New Tokens" ) temperature = gr.Slider( minimum=0.10, maximum=4.00, value=0.70, label="Temperature" ) top_p = gr.Slider( minimum=0.10, maximum=1.00, value=0.90, label="Top-p (nucleus sampling)" ) max_new_tokens = gr.Slider(1, 4000, 200) temperature = gr.Slider(0.1, 4.0, 0.7) top_p = gr.Slider(0.1, 1.0, 0.9)
chat_history = gr.State([])
# Event handling submit_btn.click( chatbot, [msg, chat_history, model_choice, system_message, max_new_tokens, temperature, top_p],app.py+35-44