import gradio as gr import requests import logging import os # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Load Hugging Face API token from environment variables (replace with a direct token if needed) HF_TOKEN = os.getenv('HF_TOKEN') # Default model DEFAULT_MODEL = "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B" # Default system message DEFAULT_SYSTEM_MESSAGE = "You are SoniXAI, an AI specializing in curating playlists based on user mood and answering music-related queries. Your responses should be engaging, friendly, and focused on enhancing the user's music experience. Current Song Catalog: Millionaire – Honey Singh, As It Was – Harry Styles, All Black – Raftaar, Bling Bang Bang Born, Video Killed the Radio Star, Karma Police, Jaan 'Nissar, Kabhi Kabhi Aditi, Iluminati. Mood-Based Playlists: If a user expresses an emotion (e.g., 'I'm sad,' 'I need energy,' 'Feeling romantic'), classify the available songs into fitting moods and suggest a playlist. Song Queries: If a user asks about a specific song in the catalog, provide details or suggest it. Engagement: Offer suggestions in a conversational manner, e.g., 'I sense you're feeling low. Want me to spin up a chill playlist? Here's what I got for you: Karma Police and Jaan 'Nissar!' 'Need a hype boost? Let’s go! I’ve got Millionaire and All Black ready to set the vibe.' Your goal is to be an intelligent music companion, guiding users with relevant recommendations while keeping the experience smooth and enjoyable." # Hugging Face API request function def query_huggingface(model, system_message, user_input, max_tokens, temperature, top_p): """Query the Hugging Face model API""" headers = {"Authorization": f"Bearer {HF_TOKEN}"} api_url = f"https://api-inference.huggingface.co/models/{model}" # Construct full prompt correctly full_prompt = f"{system_message}\n\nUser: {user_input}\nAssistant:" payload = { "inputs": full_prompt, "parameters": { "max_new_tokens": max_tokens, "temperature": temperature, "top_p": top_p, "return_full_text": False # Ensuring only the new response is returned } } logger.info(f"Sending request to {api_url} with payload: {payload}") response = requests.post(api_url, headers=headers, json=payload) if response.status_code == 200: output = response.json() # Ensure proper parsing of the response if isinstance(output, list) and len(output) > 0: generated_text = output[0].get("generated_text", "").strip() # Fix possible artifacts like or duplicate responses generated_text = generated_text.split("")[-1].strip() # Remove unwanted tags return generated_text else: return "Error: Unexpected response from the model." else: return f"Error: API request failed with status code {response.status_code}." # Gradio interface function def chat(user_input, model, system_message, max_tokens, temperature, top_p): """Handles chat interactions""" return query_huggingface(model, system_message, user_input, max_tokens, temperature, top_p) # Gradio UI using Interface (instead of ChatInterface) with gr.Blocks() as demo: gr.Markdown("# 🤖 DeepSeek Chatbot (Gradio Version)") with gr.Row(): model_dropdown = gr.Dropdown( choices=["deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"], value=DEFAULT_MODEL, label="Select Model" ) system_message_input = gr.Textbox( value=DEFAULT_SYSTEM_MESSAGE, label="System Message", lines=3 ) with gr.Row(): max_tokens_slider = gr.Slider(minimum=10, maximum=4000, value=100, step=10, label="Max Tokens") temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.3, step=0.1, label="Temperature") top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.6, step=0.1, label="Top-p") chatbox = gr.Textbox(label="User Input") response_box = gr.Textbox(label="Chatbot Response", interactive=False) submit_button = gr.Button("Send") submit_button.click( chat, inputs=[chatbox, model_dropdown, system_message_input, max_tokens_slider, temperature_slider, top_p_slider], outputs=response_box ) # Launch the app if __name__ == "__main__": demo.launch()