CoolFace
Apppublic

Manojajj/CodeAssistant-Qwen

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
1likes
app.py103 linesDownload Raw Back to root
1import gradio as gr2from huggingface_hub import InferenceClient3 4# Initialize a list to store the conversation history5conversation_history = []6 7# Function to interact with the model using the Inference API8def chat_with_model(user_input, hf_api_key):9    global conversation_history10    11    if not hf_api_key:12        return "Error: Please provide your Hugging Face API key."13 14    try:15        # Initialize the InferenceClient with the provided API key16        client = InferenceClient(api_key=hf_api_key)17 18        # Add the user's message to the conversation history19        conversation_history.append({"role": "user", "content": user_input})20 21        # Define the system message (defining the assistant role)22        system_message = {23            "role": "system", 24            "content": "You are a code assistant that helps with code generation, debugging, and explanations."25        }26 27        # Add system message to the conversation history28        if len(conversation_history) == 1:  # Add system message only once29            conversation_history.insert(0, system_message)30 31        # Ensure the conversation history doesn't exceed token limits32        if len(conversation_history) > 10:  # Keep the last 10 messages33            conversation_history = [system_message] + conversation_history[-10:]34 35        # Create a stream for chat completions using the API36        stream = client.chat.completions.create(37            model="Qwen/Qwen2.5-Coder-32B-Instruct", 38            messages=conversation_history, 39            max_tokens=500,40            stream=True41        )42        43        # Collect the generated response from the model44        response = ""45        for chunk in stream:46            response += chunk.choices[0].delta.content47        48        # Add the assistant's response to the conversation history49        conversation_history.append({"role": "assistant", "content": response})50        51        return response52 53    except Exception as e:54        return f"Error: {e}"55 56# Create the Gradio interface with a light theme and black text57with gr.Blocks(58    css="""59        body {60            background-color: #f9f9f9;61            color: #000000;62        }63        .gradio-container {64            background-color: #ffffff;65            color: #000000;66        }67        .gradio-container input, .gradio-container textarea, .gradio-container button {68            color: #000000;69            background-color: #ffffff;70            border: 1px solid #cccccc;71        }72        .gradio-container textarea::placeholder, .gradio-container input::placeholder {73            color: #777777;74        }75        .gradio-container button {76            background-color: #f0f0f0;77            border: 1px solid #cccccc;78        }79        .gradio-container button:hover {80            background-color: #e6e6e6;81        }82        #title, #description {83            color: #000000 !important;84        }85    """86) as demo:87    gr.Markdown("<h1 id='title'>Code Assistant with Qwen2.5-Coder</h1>")88    gr.Markdown("<p id='description'>Ask me anything about coding! Enter your Hugging Face API key to start.</p>")89    90    # Create the input and output interface91    with gr.Row():92        user_input = gr.Textbox(lines=5, placeholder="Ask me anything about coding...")93        api_key = gr.Textbox(lines=1, placeholder="Enter your Hugging Face API key", type="password")94    95    # Create the output display96    output = gr.Textbox(label="Response")97 98    # Button for submitting queries99    submit_button = gr.Button("Submit")100    submit_button.click(chat_with_model, inputs=[user_input, api_key], outputs=output)101 102# Launch the app103demo.launch()