File size: 3,651 Bytes
6f7d7b4
4062a25
6f7d7b4
9178b0a
0f347bb
 
4062a25
 
0f347bb
 
9178b0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35c839b
9969cd9
 
 
35c839b
 
9969cd9
 
35c839b
 
9969cd9
 
35c839b
 
 
9969cd9
 
35c839b
9969cd9
 
35c839b
 
9969cd9
 
35c839b
9969cd9
1afe94e
35c839b
1afe94e
9969cd9
 
1afe94e
 
0f347bb
9178b0a
 
 
 
0f347bb
9178b0a
 
6f7d7b4
9178b0a
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import gradio as gr
from huggingface_hub import InferenceClient

# Initialize a list to store the conversation history
conversation_history = []

# Function to interact with the model using the Inference API
def chat_with_model(user_input, hf_api_key):
    global conversation_history
    
    if not hf_api_key:
        return "Error: Please provide your Hugging Face API key."

    try:
        # Initialize the InferenceClient with the provided API key
        client = InferenceClient(api_key=hf_api_key)

        # Add the user's message to the conversation history
        conversation_history.append({"role": "user", "content": user_input})

        # Define the system message (defining the assistant role)
        system_message = {
            "role": "system", 
            "content": "You are a code assistant that helps with code generation, debugging, and explanations."
        }

        # Add system message to the conversation history
        if len(conversation_history) == 1:  # Add system message only once
            conversation_history.insert(0, system_message)

        # Ensure the conversation history doesn't exceed token limits
        if len(conversation_history) > 10:  # Keep the last 10 messages
            conversation_history = [system_message] + conversation_history[-10:]

        # Create a stream for chat completions using the API
        stream = client.chat.completions.create(
            model="Qwen/Qwen2.5-Coder-32B-Instruct", 
            messages=conversation_history, 
            max_tokens=500,
            stream=True
        )
        
        # Collect the generated response from the model
        response = ""
        for chunk in stream:
            response += chunk.choices[0].delta.content
        
        # Add the assistant's response to the conversation history
        conversation_history.append({"role": "assistant", "content": response})
        
        return response

    except Exception as e:
        return f"Error: {e}"

# Create the Gradio interface with a light theme and black text
with gr.Blocks(
    css="""
        body {
            background-color: #f9f9f9;
            color: #000000;
        }
        .gradio-container {
            background-color: #ffffff;
            color: #000000;
        }
        .gradio-container input, .gradio-container textarea, .gradio-container button {
            color: #000000;
            background-color: #ffffff;
            border: 1px solid #cccccc;
        }
        .gradio-container textarea::placeholder, .gradio-container input::placeholder {
            color: #777777;
        }
        .gradio-container button {
            background-color: #f0f0f0;
            border: 1px solid #cccccc;
        }
        .gradio-container button:hover {
            background-color: #e6e6e6;
        }
        #title, #description {
            color: #000000 !important;
        }
    """
) as demo:
    gr.Markdown("<h1 id='title'>Code Assistant with Qwen2.5-Coder</h1>")
    gr.Markdown("<p id='description'>Ask me anything about coding! Enter your Hugging Face API key to start.</p>")
    
    # Create the input and output interface
    with gr.Row():
        user_input = gr.Textbox(lines=5, placeholder="Ask me anything about coding...")
        api_key = gr.Textbox(lines=1, placeholder="Enter your Hugging Face API key", type="password")
    
    # Create the output display
    output = gr.Textbox(label="Response")

    # Button for submitting queries
    submit_button = gr.Button("Submit")
    submit_button.click(chat_with_model, inputs=[user_input, api_key], outputs=output)

# Launch the app
demo.launch()