Update app.py
Browse files123456778910101112136 unmodified lines20212223242324252526272827293031322 unmodified lines3536373638394041424344434546474849505152535455569 unmodified lines66676869617071727336 unmodified lines110111112113114115116117118119import gradio as grimport spacesfrom functools import lru_cache
# 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" )
6 unmodified lines
# --- Chatbot function ---def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p): # If history is empty, initialize it as a list if history is None: history = history or [] # Get the selected model component history = []
# Select the model model_component = MODELS[model_choice]
# Create payload for the model payload = { "messages": [{"role": "user", "content": input_text}],2 unmodified lines "temperature": temperature, "top_p": top_p }
# Run inference using the selected model try: response = model_component(payload) assistant_response = response[-1]["content"] except Exception as e: assistant_response = f"Error: {str(e)}"
# Append user and assistant messages in the new format history.append({"role": "user", "content": input_text}) history.append({"role": "assistant", "content": assistant_response})
# Return the updated conversation to display and store # 1) chatbot_output = updated history of messages # 2) chat_history = same updated history (as state) # 3) "" to clear the input textbox return history, history, ""
# --- Gradio Interface ---9 unmodified lines
with gr.Row(): with gr.Column(): # Use type='messages' for OpenAI-style messages chatbot_output = gr.Chatbot(label="DeepSeek Chatbot", height=500, type='messages') chatbot_output = gr.Chatbot(label="DeepSeek Chatbot", height=500, type="messages") msg = gr.Textbox(label="Your Message", placeholder="Type your message here...") with gr.Row(): submit_btn = gr.Button("Submit", variant="primary")36 unmodified lines [chatbot_output, chat_history, msg] )
# (Optional) Remove or modify references to spaces.GPU() if you do not need GPU managementif __name__ == "__main__": # Just launch regularly if you don't need spaces.GPU() for hardware acceleration demo.launch()
# If you require GPU on Hugging Face Spaces, you can wrap demo.launch like so instead: # spaces.GPU()(demo.launch)()app.py+23-9