Spaces:
Runtime error
Runtime error
Commit
Β·
d722176
1
Parent(s):
b6f4d2d
update
Browse files- app.py +61 -18
- requirements.txt +4 -3
app.py
CHANGED
@@ -1,25 +1,68 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
-
# Load
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import tempfile
|
3 |
+
import subprocess
|
4 |
+
import shutil
|
5 |
import gradio as gr
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
7 |
import torch
|
8 |
|
9 |
+
# Load Qwen code model
|
10 |
+
model_name = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")
|
13 |
|
14 |
+
def clone_and_read_repo(repo_url):
|
15 |
+
"""Clone the repo and return concatenated text of selected files."""
|
16 |
+
temp_dir = tempfile.mkdtemp()
|
17 |
+
try:
|
18 |
+
subprocess.run(["git", "clone", "--depth", "1", repo_url, temp_dir], check=True)
|
19 |
+
repo_text = ""
|
20 |
+
for root, dirs, files in os.walk(temp_dir):
|
21 |
+
for file in files:
|
22 |
+
if file.endswith((".py", ".js", ".ts", ".md", ".txt", ".java", ".c", ".cpp")):
|
23 |
+
file_path = os.path.join(root, file)
|
24 |
+
try:
|
25 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
26 |
+
repo_text += f"\n\n# File: {os.path.relpath(file_path, temp_dir)}\n" + f.read()
|
27 |
+
except:
|
28 |
+
pass
|
29 |
+
return repo_text[:50000] # limit for model context
|
30 |
+
finally:
|
31 |
+
shutil.rmtree(temp_dir)
|
32 |
|
33 |
+
def generate_code(user_code, repo_url):
|
34 |
+
# Add repo content as extra context
|
35 |
+
context = ""
|
36 |
+
if repo_url and repo_url.strip():
|
37 |
+
try:
|
38 |
+
context = clone_and_read_repo(repo_url)
|
39 |
+
except Exception as e:
|
40 |
+
context = f"[Error fetching repo: {e}]"
|
41 |
+
|
42 |
+
full_prompt = ""
|
43 |
+
if context:
|
44 |
+
full_prompt += f"Here is the repository context:\n{context}\n\n"
|
45 |
+
full_prompt += f"User request/code:\n{user_code}"
|
46 |
|
47 |
+
messages = [
|
48 |
+
{"role": "system", "content": "You are a helpful coding assistant."},
|
49 |
+
{"role": "user", "content": full_prompt}
|
50 |
+
]
|
51 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
52 |
+
inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
53 |
+
outputs = model.generate(**inputs, max_new_tokens=512)
|
54 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
55 |
+
|
56 |
+
iface = gr.Interface(
|
57 |
+
fn=generate_code,
|
58 |
+
inputs=[
|
59 |
+
gr.Code(label="Write Code or Prompt", language="python", placeholder="# Type your code or question here"),
|
60 |
+
gr.Textbox(label="GitHub Repo URL (optional)", placeholder="https://github.com/user/repo")
|
61 |
+
],
|
62 |
+
outputs=gr.Code(label="Generated Answer", language="python"),
|
63 |
+
title="Qwen-Coder with Git Repo Context & Monaco Editor",
|
64 |
+
description="Ask questions or provide code, optionally fetch a GitHub repo as context. Uses a Monaco code editor for input and output."
|
65 |
+
)
|
66 |
+
|
67 |
+
if __name__ == "__main__":
|
68 |
+
iface.launch()
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
-
transformers
|
2 |
-
|
3 |
-
|
|
|
|
1 |
+
transformers>=4.37.0
|
2 |
+
gradio>=4.21.0
|
3 |
+
torch>=2.1.0
|
4 |
+
gitpython
|