Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,49 +2,45 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# β
Load
|
| 6 |
model_name = "bigcode/santacoder"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
| 9 |
|
| 10 |
-
# β
|
| 11 |
def code_assistant(code, instruction, language):
|
| 12 |
if not code.strip() or not instruction.strip():
|
| 13 |
-
return "β οΈ Please
|
| 14 |
|
| 15 |
-
|
| 16 |
-
##
|
| 17 |
-
### Code:
|
| 18 |
-
{code}
|
| 19 |
-
### Suggestion:
|
| 20 |
-
"""
|
| 21 |
|
| 22 |
-
|
| 23 |
-
input_ids = inputs["input_ids"].to(model.device)
|
| 24 |
|
| 25 |
with torch.no_grad():
|
| 26 |
output_ids = model.generate(
|
| 27 |
-
input_ids
|
| 28 |
-
max_new_tokens=
|
| 29 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 30 |
do_sample=True,
|
| 31 |
-
temperature=0.
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
full_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
suggestion = full_output.split("### Suggestion:")[-1].strip()
|
| 39 |
-
else:
|
| 40 |
-
suggestion = full_output[len(prompt):].strip()
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="βοΈ
|
| 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")
|