Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,50 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
|
5 |
+
def create_chat_interface():
|
6 |
+
# Initialize model and tokenizer
|
7 |
+
model_name = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_name,
|
11 |
+
device_map="auto",
|
12 |
+
trust_remote_code=True
|
13 |
+
)
|
14 |
+
|
15 |
+
# Chat function
|
16 |
+
def chat(message, history):
|
17 |
+
# Format input with chat template
|
18 |
+
prompt = f"User: {message}\nAssistant:"
|
19 |
+
|
20 |
+
# Generate response
|
21 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
22 |
+
outputs = model.generate(
|
23 |
+
**inputs,
|
24 |
+
max_new_tokens=512,
|
25 |
+
temperature=0.7,
|
26 |
+
num_return_sequences=1
|
27 |
+
)
|
28 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
+
|
30 |
+
return response
|
31 |
+
|
32 |
+
# Create Gradio interface
|
33 |
+
interface = gr.ChatInterface(
|
34 |
+
fn=chat,
|
35 |
+
title="Code Assistant Chat",
|
36 |
+
description="Ask coding questions or get help with programming tasks.",
|
37 |
+
theme=gr.themes.Soft(),
|
38 |
+
examples=[
|
39 |
+
"Write a Python function to sort a list",
|
40 |
+
"How do I read a CSV file in pandas?",
|
41 |
+
"Explain object-oriented programming concepts"
|
42 |
+
]
|
43 |
+
)
|
44 |
+
|
45 |
+
return interface
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
# Launch the interface
|
49 |
+
chat_app = create_chat_interface()
|
50 |
+
chat_app.launch(share=True)
|