Spaces:
Runtime error
Runtime error
import os | |
import tempfile | |
import subprocess | |
import shutil | |
import gradio as gr | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import torch | |
# Load Qwen code model | |
model_name = "Qwen/Qwen2.5-Coder-32B-Instruct" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto") | |
def clone_and_read_repo(repo_url): | |
"""Clone the repo and return concatenated text of selected files.""" | |
temp_dir = tempfile.mkdtemp() | |
try: | |
subprocess.run(["git", "clone", "--depth", "1", repo_url, temp_dir], check=True) | |
repo_text = "" | |
for root, dirs, files in os.walk(temp_dir): | |
for file in files: | |
if file.endswith((".py", ".js", ".ts", ".md", ".txt", ".java", ".c", ".cpp")): | |
file_path = os.path.join(root, file) | |
try: | |
with open(file_path, "r", encoding="utf-8") as f: | |
repo_text += f"\n\n# File: {os.path.relpath(file_path, temp_dir)}\n" + f.read() | |
except: | |
pass | |
return repo_text[:50000] # limit for model context | |
finally: | |
shutil.rmtree(temp_dir) | |
def generate_code(user_code, repo_url): | |
# Add repo content as extra context | |
context = "" | |
if repo_url and repo_url.strip(): | |
try: | |
context = clone_and_read_repo(repo_url) | |
except Exception as e: | |
context = f"[Error fetching repo: {e}]" | |
full_prompt = "" | |
if context: | |
full_prompt += f"Here is the repository context:\n{context}\n\n" | |
full_prompt += f"User request/code:\n{user_code}" | |
messages = [ | |
{"role": "system", "content": "You are a helpful coding assistant."}, | |
{"role": "user", "content": full_prompt} | |
] | |
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
inputs = tokenizer([text], return_tensors="pt").to(model.device) | |
outputs = model.generate(**inputs, max_new_tokens=512) | |
return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
iface = gr.Interface( | |
fn=generate_code, | |
inputs=[ | |
gr.Code(label="Write Code or Prompt", language="python", placeholder="# Type your code or question here"), | |
gr.Textbox(label="GitHub Repo URL (optional)", placeholder="https://github.com/user/repo") | |
], | |
outputs=gr.Code(label="Generated Answer", language="python"), | |
title="Qwen-Coder with Git Repo Context & Monaco Editor", | |
description="Ask questions or provide code, optionally fetch a GitHub repo as context. Uses a Monaco code editor for input and output." | |
) | |
if __name__ == "__main__": | |
iface.launch() |