File size: 1,530 Bytes
d7013ad
7f2db7d
 
d7013ad
7f2db7d
 
a6eb078
7f2db7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

def create_chat_interface():
    # Initialize model and tokenizer
    model_name = "Qwen/Qwen2.5-Coder-7B-Instruct"
    tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(
        model_name,
        device_map="auto",
        trust_remote_code=True
    )

    # Chat function
    def chat(message, history):
        # Format input with chat template
        prompt = f"User: {message}\nAssistant:"
        
        # Generate response
        inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
        outputs = model.generate(
            **inputs,
            max_new_tokens=512,
            temperature=0.7,
            num_return_sequences=1
        )
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        return response

    # Create Gradio interface
    interface = gr.ChatInterface(
        fn=chat,
        title="Code Assistant Chat",
        description="Ask coding questions or get help with programming tasks.",
        theme=gr.themes.Soft(),
        examples=[
            "Write a Python function to sort a list",
            "How do I read a CSV file in pandas?",
            "Explain object-oriented programming concepts"
        ]
    )
    
    return interface

if __name__ == "__main__":
    # Launch the interface
    chat_app = create_chat_interface()
    chat_app.launch(share=True)