Update app.py
Browse files123456789101112136 unmodified lines20212223242526272829302 unmodified lines333435363738394041424344454647489 unmodified lines5859606162636436 unmodified lines101102103104105import gradio as grfrom 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_gradioreturn gr.load(name=f"deepseek-ai/{model_name}",src="huggingface", # Changed from transformers_gradio.registryapi_name="/chat")6 unmodified lines# --- 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 = {"messages": [{"role": "user", "content": input_text}],2 unmodified lines"temperature": temperature,"top_p": top_p}# Run inference using the selected modeltry:response = model_component(payload)assistant_response = response[-1]["content"]except Exception as e:assistant_response = f"Error: {str(e)}"history.append({"role": "user", "content": input_text})history.append({"role": "assistant", "content": assistant_response})return history, history, ""# --- Gradio Interface ---9 unmodified lineswith gr.Row():with gr.Column():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])if __name__ == "__main__":demo.launch()123456789101112136 unmodified lines202122232425262728293031322 unmodified lines353637383940414243444546474849505152535455569 unmodified lines666768697071727336 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):return gr.load(name=f"deepseek-ai/{model_name}",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 listif history is None:history = []# Select the modelmodel_component = MODELS[model_choice]# Create payload for the modelpayload = {"messages": [{"role": "user", "content": input_text}],2 unmodified lines"temperature": temperature,"top_p": top_p}# Run inference using the selected modeltry: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 formathistory.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 textboxreturn history, history, ""# --- Gradio Interface ---9 unmodified lineswith gr.Row():with gr.Column():# Use type='messages' for OpenAI-style messageschatbot_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 accelerationdemo.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