File size: 1,433 Bytes
6f7d7b4
8a4c070
6f7d7b4
8a4c070
 
6f7d7b4
8a4c070
 
 
 
6f7d7b4
8a4c070
 
6f7d7b4
8a4c070
 
6f7d7b4
8a4c070
 
6f7d7b4
8a4c070
6f7d7b4
8a4c070
6f7d7b4
8a4c070
 
6f7d7b4
 
8a4c070
6f7d7b4
 
8a4c070
 
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
import gradio as gr
from transformers import pipeline

# Initialize the pipeline for code generation and assistance
pipe = pipeline("text-generation", model="Qwen/Qwen2.5-Coder-32B-Instruct")

# Function to interact with the model for code-related assistance
def code_assistance(user_input):
    # Define the system message to set the context
    system_message = "You are Qwen, a code assistant created by Alibaba Cloud. You assist with code generation, debugging, and explanation tasks."
    
    # Format the prompt with the system message and user input (code-related query)
    prompt = f"{system_message}\nUser: {user_input}\nAssistant (Code Assistance):"
    
    # Use the pipeline to generate the response for code assistance
    response = pipe(prompt, max_length=512, num_return_sequences=1)
    
    # Extract and clean the response to return only the assistant's code suggestion or explanation
    generated_response = response[0]['generated_text'].split("Assistant (Code Assistance):")[1].strip()
    
    return generated_response

# Create the Gradio interface for the code assistance chatbot
iface = gr.Interface(
    fn=code_assistance, 
    inputs=gr.Textbox(lines=5, placeholder="Ask for code help..."), 
    outputs="text", 
    title="Qwen2.5-Coder Chatbot", 
    description="A chatbot using Qwen2.5-Coder for code generation, debugging, and explanation tasks."
)

# Launch the Gradio interface
iface.launch()