Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041425 unmodified lines4849505152535414 unmodified lines69707172737475767726 unmodified lines104105106107import gradio as grfrom functools import lru_cache# Cache model loading to optimize performance@lru_cache(maxsize=3)def load_hf_model(model_name):return gr.load(f"models/{model_name}", src="huggingface")# Load all models at startupMODELS = {"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B": load_hf_model("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"),"deepseek-ai/DeepSeek-R1": load_hf_model("deepseek-ai/DeepSeek-R1"),"deepseek-ai/DeepSeek-R1-Zero": load_hf_model("deepseek-ai/DeepSeek-R1-Zero")}# --- Chatbot function ---def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):history = history or []# Get the selected model componentmodel_component = MODELS[model_choice]# Create payload for the modelpayload = {"inputs": input_text, # Directly pass the input text"parameters": {"max_new_tokens": max_new_tokens,"temperature": temperature,"top_p": top_p,"return_full_text": False # Only return the generated text}}# Run inference using the selected modeltry:response = model_component(**payload) # Pass payload as keyword argumentsif isinstance(response, list) and len(response) > 0:# Extract the generated text from the responseassistant_response = response[0].get("generated_text", "No response generated.")else:assistant_response = "Unexpected model response format."except Exception as e:5 unmodified linesreturn history, history, ""# --- Gradio Interface ---with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as demo:gr.Markdown("""# DeepSeek Chatbot14 unmodified lineswith gr.Row():with gr.Accordion("Options", open=True):model_choice = gr.Radio(choices=list(MODELS.keys()),label="Choose a Model",value="deepseek-ai/DeepSeek-R1")with gr.Accordion("Optional Parameters", open=False):system_message = gr.Textbox(26 unmodified lines)if __name__ == "__main__":demo.launch()1234567891011121314151617181920212223245 unmodified lines3031323334353614 unmodified lines51525354555657585926 unmodified lines86878889import gradio as grfrom models import demo # Import the demo object from models.py# --- Chatbot function ---def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):history = history or []# Create payload for the modelpayload = {"messages": [{"role": "user", "content": input_text}],"system": system_message,"max_tokens": max_new_tokens,"temperature": temperature,"top_p": top_p}# Run inference using the selected modeltry:response = demo(payload) # Use the demo object directlyif isinstance(response, dict) and "choices" in response:assistant_response = response["choices"][0]["message"]["content"]else:assistant_response = "Unexpected model response format."except Exception as e:5 unmodified linesreturn history, history, ""# --- Gradio Interface ---with gr.Blocks(theme=gr.themes.Soft(), title="DeepSeek Chatbot") as app:gr.Markdown("""# DeepSeek Chatbot14 unmodified lineswith gr.Row():with gr.Accordion("Options", open=True):model_choice = gr.Radio(choices=["DeepSeek-R1-Distill-Qwen-32B", "DeepSeek-R1", "DeepSeek-R1-Zero"],label="Choose a Model",value="DeepSeek-R1")with gr.Accordion("Optional Parameters", open=False):system_message = gr.Textbox(26 unmodified lines)if __name__ == "__main__":app.launch()
app.py+13-31