Ayyyesha commited on
Commit
ed512f2
Β·
verified Β·
1 Parent(s): 6633603

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -25
app.py CHANGED
@@ -2,49 +2,45 @@ import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
- # βœ… Load SantaCoder model
6
  model_name = "bigcode/santacoder"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name)
 
9
 
10
- # βœ… Define the assistant function
11
  def code_assistant(code, instruction, language):
12
  if not code.strip() or not instruction.strip():
13
- return "⚠️ Please enter both code and instruction."
14
 
15
- prompt = f"""### Language: {language}
16
- ### Instruction: {instruction}
17
- ### Code:
18
- {code}
19
- ### Suggestion:
20
- """
21
 
22
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
23
- input_ids = inputs["input_ids"].to(model.device)
24
 
25
  with torch.no_grad():
26
  output_ids = model.generate(
27
- input_ids=input_ids,
28
- max_new_tokens=256,
29
- pad_token_id=tokenizer.eos_token_id,
30
  do_sample=True,
31
- temperature=0.7
 
32
  )
33
 
34
  full_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
35
 
36
- # Extract only the suggestion
37
- if "### Suggestion:" in full_output:
38
- suggestion = full_output.split("### Suggestion:")[-1].strip()
39
- else:
40
- suggestion = full_output[len(prompt):].strip()
41
 
42
- return suggestion or "⚠️ No suggestion generated. Try again with better instruction."
 
 
 
 
43
 
44
  # βœ… Gradio UI
45
  with gr.Blocks() as demo:
46
  gr.Markdown("## πŸ€– Multilingual AI Code Assistant")
47
- gr.Markdown("Paste your code, describe what you want (e.g., Explain, Fix bugs), and get instant help!")
48
 
49
  with gr.Row():
50
  lang = gr.Dropdown(
@@ -53,8 +49,8 @@ with gr.Blocks() as demo:
53
  value="Python"
54
  )
55
  task = gr.Textbox(label="πŸ“Œ Task (Explain, Fix, Improve...)", placeholder="Explain this code")
56
-
57
- code_input = gr.Code(label="✍️ Your Code", language="python")
58
  output = gr.Textbox(label="πŸ’‘ AI Suggestion", lines=10)
59
 
60
  submit = gr.Button("πŸš€ Generate Suggestion")
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
+ # βœ… Load model and tokenizer
6
  model_name = "bigcode/santacoder"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
9
+ model.eval()
10
 
11
+ # βœ… Function to generate suggestions
12
  def code_assistant(code, instruction, language):
13
  if not code.strip() or not instruction.strip():
14
+ return "⚠️ Please provide both code and instruction."
15
 
16
+ # Clear and short prompt
17
+ prompt = f"# Language: {language}\n# Task: {instruction}\n{code}\n# Suggestion:\n"
 
 
 
 
18
 
19
+ input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids.to(model.device)
 
20
 
21
  with torch.no_grad():
22
  output_ids = model.generate(
23
+ input_ids,
24
+ max_new_tokens=128,
 
25
  do_sample=True,
26
+ temperature=0.6,
27
+ pad_token_id=tokenizer.eos_token_id,
28
  )
29
 
30
  full_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
31
 
32
+ # Remove prompt from output
33
+ suggestion = full_output.replace(prompt, "").strip()
 
 
 
34
 
35
+ # Avoid garbage suggestions
36
+ if "Constant" in suggestion[:30]:
37
+ return "⚠️ Couldn't generate a valid suggestion. Try rephrasing your instruction."
38
+
39
+ return suggestion or "⚠️ No meaningful suggestion generated."
40
 
41
  # βœ… Gradio UI
42
  with gr.Blocks() as demo:
43
  gr.Markdown("## πŸ€– Multilingual AI Code Assistant")
 
44
 
45
  with gr.Row():
46
  lang = gr.Dropdown(
 
49
  value="Python"
50
  )
51
  task = gr.Textbox(label="πŸ“Œ Task (Explain, Fix, Improve...)", placeholder="Explain this code")
52
+
53
+ code_input = gr.Code(label="✍️ Paste your code here", language="python")
54
  output = gr.Textbox(label="πŸ’‘ AI Suggestion", lines=10)
55
 
56
  submit = gr.Button("πŸš€ Generate Suggestion")