prasenjeet099 commited on
Commit
d722176
Β·
1 Parent(s): b6f4d2d
Files changed (2) hide show
  1. app.py +61 -18
  2. 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 model and tokenizer
6
- model_id = "prasenjeethowlader099/zetallm_4"
 
 
7
 
8
- tokenizer = AutoTokenizer.from_pretrained(model_id)
9
- model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda" if torch.cuda.is_available() else "cpu")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Chat function
12
- def chat(prompt, history=[]):
13
- input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
14
- output = model.generate(input_ids, max_new_tokens=200, do_sample=True, top_k=50, top_p=0.95, temperature=0.7)
15
- response = tokenizer.decode(output[0], skip_special_tokens=True)
16
- return response
 
 
 
 
 
 
 
17
 
18
- # Gradio UI
19
- gr.Interface(
20
- fn=chat,
21
- inputs=gr.Textbox(lines=5, label="Ask Zeeta"),
22
- outputs=gr.Textbox(label="Zeeta's Response"),
23
- title="CodeAgent Zeeta LLM",
24
- description="Chat with prasenjeethowlader099/zetallm_4"
25
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- torch
3
- gradio
 
 
1
+ transformers>=4.37.0
2
+ gradio>=4.21.0
3
+ torch>=2.1.0
4
+ gitpython