Update app.py
Browse files123456789101112131415161718192021221 unmodified line2425262728293031323334353637383940414243444517 unmodified lines6364656667686914 unmodified lines84858687888990import gradio as gr# Placeholder for model loading (adjust as needed for your specific models)def load_model(model_name):print(f"Loading {model_name}...")# Simulate different model behaviors (replace with actual model logic)if model_name == "DeepSeek-R1-Distill-Qwen-32B":return lambda input_text, history: f"Distilled Model Response to: {input_text}"elif model_name == "DeepSeek-R1":return lambda input_text, history: f"Base Model Response to: {input_text}"elif model_name == "DeepSeek-R1-Zero":return lambda input_text, history: f"Zero Model Response to: {input_text}"else:return lambda input_text, history: f"Default Response to: {input_text}"# Load the models (placeholder functions here)deepseek_r1_distill = load_model("DeepSeek-R1-Distill-Qwen-32B")deepseek_r1 = load_model("DeepSeek-R1")deepseek_r1_zero = load_model("DeepSeek-R1-Zero")# --- Chatbot function ---def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):1 unmodified lineprint(f"Input: {input_text}, History: {history}, Model: {model_choice}")# Choose the model based on user selectionif model_choice == "DeepSeek-R1-Distill-Qwen-32B":model_function = deepseek_r1_distillelif model_choice == "DeepSeek-R1":model_function = deepseek_r1elif model_choice == "DeepSeek-R1-Zero":model_function = deepseek_r1_zeroelse:model_function = lambda x, h: "Please select a model."# Simulate model response with parametersresponse = model_function(input_text, history)# Format the response for display (without parameter details in the main chat)display_response = f"{response}"history.append((input_text, display_response))return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p # Clear input, keep other parameters# --- Gradio Interface ---with gr.Blocks(theme=gr.themes.Soft()) as demo:17 unmodified lines# Options moved below the chat interfacewith gr.Row():with gr.Accordion("Options", open=True): # Changed label to "Options"model_choice = gr.Radio(choices=["DeepSeek-R1-Distill-Qwen-32B", "DeepSeek-R1", "DeepSeek-R1-Zero"],label="Choose a Model",14 unmodified linestop_p = gr.Slider(minimum=0.10, maximum=1.00, value=0.90, label="Top-p (nucleus sampling)")# Maintain chat historychat_history = gr.State([])123456789101112131415161718192021222324252627282930313233341 unmodified line363738394041424344454647484950515253545556575859606162636417 unmodified lines8283848586878814 unmodified lines103104105106107108import gradio as grimport subprocess# Function to load a model using Hugging Face Spaces and enable GPUdef load_model_with_gpu(model_name):print(f"Attempting to load {model_name} with GPU enabled...")try:# Use subprocess to run hf.space_info and get GPU settingresult = subprocess.run(["python", "-c", f"from huggingface_hub import space_info; print(space_info('{model_name}').hardware)"],capture_output=True,text=True,check=True)hardware = result.stdout.strip()print(f"Hardware for {model_name}: {hardware}")demo = gr.load(name=model_name, src="spaces")# Return the loaded model demoprint(f"Successfully loaded {model_name}")return demoexcept Exception as e:print(f"Error loading model {model_name}: {e}")return None# Load the models with GPU enabled (if available)deepseek_r1_distill = load_model_with_gpu("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B")deepseek_r1 = load_model_with_gpu("deepseek-ai/DeepSeek-R1")deepseek_r1_zero = load_model_with_gpu("deepseek-ai/DeepSeek-R1-Zero")# --- Chatbot function ---def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):1 unmodified lineprint(f"Input: {input_text}, History: {history}, Model: {model_choice}")# Choose the model based on user selectionif model_choice == "DeepSeek-R1-Distill-Qwen-32B" and deepseek_r1_distill:model_demo = deepseek_r1_distillelif model_choice == "DeepSeek-R1" and deepseek_r1:model_demo = deepseek_r1elif model_choice == "DeepSeek-R1-Zero" and deepseek_r1_zero:model_demo = deepseek_r1_zeroelse:default_response = "Model not selected or could not be loaded."history.append((input_text, default_response))return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p# Adjust the call to the model, remove default_value if not applicablemodel_output = model_demo(input_text, history, max_new_tokens, temperature, top_p, system_message)# Check if model_output is iterable and has expected number of elementsif not isinstance(model_output, (list, tuple)) or len(model_output) < 2:error_message = "Model output does not have the expected format."history.append((input_text, error_message))return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_presponse = model_output[-1][1] if model_output[-1][1] else "Model did not return a response."history.append((input_text, response))return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p# --- Gradio Interface ---with gr.Blocks(theme=gr.themes.Soft()) as demo:17 unmodified lines# Options moved below the chat interfacewith 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",14 unmodified linestop_p = gr.Slider(minimum=0.10, maximum=1.00, value=0.90, label="Top-p (nucleus sampling)")# Maintain chat historychat_history = gr.State([])
app.py+50-32