Upload 4 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from model import generate_response
|
| 3 |
+
from prompt import SYSTEM_PROMPT
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
def process_error(error_message: str) -> str:
|
| 7 |
+
"""
|
| 8 |
+
Main function to process the error message and return JSON response.
|
| 9 |
+
"""
|
| 10 |
+
# Build full prompt
|
| 11 |
+
full_prompt = SYSTEM_PROMPT + error_message.strip()
|
| 12 |
+
|
| 13 |
+
# Get raw model output
|
| 14 |
+
raw_output = generate_response(full_prompt)
|
| 15 |
+
|
| 16 |
+
# Extract only the JSON part (if model adds extra text)
|
| 17 |
+
# This is a simple heuristic - in practice, better parsing/validation is needed
|
| 18 |
+
try:
|
| 19 |
+
# Attempt to parse as JSON
|
| 20 |
+
parsed = json.loads(raw_output)
|
| 21 |
+
return json.dumps(parsed, indent=2)
|
| 22 |
+
except json.JSONDecodeError:
|
| 23 |
+
# If not valid JSON, return raw output (or handle as error)
|
| 24 |
+
return raw_output
|
| 25 |
+
|
| 26 |
+
# Gradio interface
|
| 27 |
+
interface = gr.Interface(
|
| 28 |
+
fn=process_error,
|
| 29 |
+
inputs=gr.Textbox(lines=5, placeholder="Paste your error or stack trace here...", label="Error Message"),
|
| 30 |
+
outputs=gr.Textbox(label="Bugfix Suggestion (JSON)"),
|
| 31 |
+
title="🐛 BugFix Assistant",
|
| 32 |
+
description="Paste an error message or stack trace to get an explanation and fix suggestion."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Launch the app
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
interface.launch()
|
model.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Initialize the model pipeline (adjust model name as needed)
|
| 10 |
+
# Using a small, CPU-friendly model like StarCoder-1B
|
| 11 |
+
MODEL_NAME = "bigcode/starcoderbase-1b"
|
| 12 |
+
|
| 13 |
+
# Get HuggingFace token from environment variable
|
| 14 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
+
|
| 16 |
+
# Initialize text generation pipeline
|
| 17 |
+
# Use 'dtype="auto"' to avoid deprecation warning
|
| 18 |
+
generator = pipeline(
|
| 19 |
+
"text-generation",
|
| 20 |
+
model=MODEL_NAME,
|
| 21 |
+
dtype="auto", # Modern way to set model dtype
|
| 22 |
+
device_map="auto", # Automatically choose device (CPU if no GPU)
|
| 23 |
+
token=HF_TOKEN # Pass token for gated models
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
def generate_response(prompt: str) -> str:
|
| 27 |
+
"""
|
| 28 |
+
Generate a response from the model based on the input prompt.
|
| 29 |
+
Returns raw model output as string.
|
| 30 |
+
"""
|
| 31 |
+
# Generate text with controlled parameters
|
| 32 |
+
outputs = generator(
|
| 33 |
+
prompt,
|
| 34 |
+
max_new_tokens=200, # Limit generation length
|
| 35 |
+
num_return_sequences=1,
|
| 36 |
+
temperature=0.3, # Lower = more deterministic
|
| 37 |
+
top_p=0.9,
|
| 38 |
+
do_sample=True
|
| 39 |
+
)
|
| 40 |
+
return outputs[0]["generated_text"]
|
prompt.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# System prompt to instruct the model to always respond in structured JSON format
|
| 2 |
+
|
| 3 |
+
SYSTEM_PROMPT = """
|
| 4 |
+
You are a helpful assistant for developers debugging code.
|
| 5 |
+
The user will provide a stack trace or error message.
|
| 6 |
+
Your task is to provide a JSON object with exactly two keys:
|
| 7 |
+
- "explanation": A concise, understandable explanation of the error (max 2 sentences)
|
| 8 |
+
- "fix_suggestion": A clear, actionable fix in code or concept (max 3 sentences)
|
| 9 |
+
|
| 10 |
+
DO NOT output anything else except valid JSON.
|
| 11 |
+
Example input:
|
| 12 |
+
"TypeError: Cannot read properties of undefined (reading 'length')"
|
| 13 |
+
|
| 14 |
+
Example output:
|
| 15 |
+
{"explanation": "The variable is undefined, so accessing 'length' throws an error.", "fix_suggestion": "Check if the variable is defined before accessing its properties. Use optional chaining (?.) or a default value."}
|
| 16 |
+
|
| 17 |
+
Input:
|
| 18 |
+
"""
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|