solved the errors
Browse files- app.py +122 -20
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,29 +1,131 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Load model
|
| 6 |
-
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
model_name,
|
| 10 |
-
device_map="auto",
|
| 11 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
# Define inference function
|
| 15 |
def generate_code(prompt):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# Create Gradio interface
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
# Set up logging
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
# Load model with error handling
|
| 11 |
+
def load_model():
|
| 12 |
+
try:
|
| 13 |
+
model_name = "deepseek-ai/deepseek-coder-1.3b-base"
|
| 14 |
+
logger.info(f"Loading model: {model_name}")
|
| 15 |
+
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
+
if tokenizer.pad_token is None:
|
| 18 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 19 |
+
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
model_name,
|
| 22 |
+
device_map="auto",
|
| 23 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 24 |
+
low_cpu_mem_usage=True
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
logger.info("Model loaded successfully!")
|
| 28 |
+
return model, tokenizer
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.error(f"Error loading model: {e}")
|
| 31 |
+
raise e
|
| 32 |
|
| 33 |
# Load model
|
| 34 |
+
model, tokenizer = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Define inference function with error handling
|
| 37 |
def generate_code(prompt):
|
| 38 |
+
try:
|
| 39 |
+
if not prompt or not prompt.strip():
|
| 40 |
+
return "Please enter a valid prompt."
|
| 41 |
+
|
| 42 |
+
logger.info(f"Generating code for prompt: {prompt[:50]}...")
|
| 43 |
+
|
| 44 |
+
# Tokenize input
|
| 45 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 46 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 47 |
+
|
| 48 |
+
# Generate code
|
| 49 |
+
with torch.no_grad():
|
| 50 |
+
outputs = model.generate(
|
| 51 |
+
**inputs,
|
| 52 |
+
max_new_tokens=200,
|
| 53 |
+
temperature=0.7,
|
| 54 |
+
do_sample=True,
|
| 55 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 56 |
+
eos_token_id=tokenizer.eos_token_id
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Decode output
|
| 60 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 61 |
+
|
| 62 |
+
# Remove the input prompt from the output
|
| 63 |
+
if generated_text.startswith(prompt):
|
| 64 |
+
generated_text = generated_text[len(prompt):].strip()
|
| 65 |
+
|
| 66 |
+
return generated_text
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
logger.error(f"Error generating code: {e}")
|
| 70 |
+
return f"Error generating code: {str(e)}"
|
| 71 |
|
| 72 |
+
# Create Gradio interface with better configuration
|
| 73 |
+
def create_interface():
|
| 74 |
+
with gr.Blocks(title="AI Coder Agent", theme=gr.themes.Soft()) as interface:
|
| 75 |
+
gr.Markdown("# 🤖 AI Coder Agent")
|
| 76 |
+
gr.Markdown("Enter your coding prompt below and let AI generate code for you!")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
with gr.Column():
|
| 80 |
+
prompt_input = gr.Textbox(
|
| 81 |
+
lines=5,
|
| 82 |
+
label="Coding Prompt",
|
| 83 |
+
placeholder="Write a Python function to calculate fibonacci numbers...",
|
| 84 |
+
max_lines=10
|
| 85 |
+
)
|
| 86 |
+
generate_btn = gr.Button("Generate Code", variant="primary")
|
| 87 |
+
|
| 88 |
+
with gr.Column():
|
| 89 |
+
output = gr.Textbox(
|
| 90 |
+
lines=15,
|
| 91 |
+
label="Generated Code",
|
| 92 |
+
max_lines=20,
|
| 93 |
+
show_copy_button=True
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Examples
|
| 97 |
+
gr.Examples(
|
| 98 |
+
examples=[
|
| 99 |
+
"Write a Python function to sort a list",
|
| 100 |
+
"Create a React component for a todo list",
|
| 101 |
+
"Write a SQL query to find the top 10 customers",
|
| 102 |
+
"Create a JavaScript function to validate email addresses"
|
| 103 |
+
],
|
| 104 |
+
inputs=prompt_input
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# Event handlers
|
| 108 |
+
generate_btn.click(
|
| 109 |
+
fn=generate_code,
|
| 110 |
+
inputs=prompt_input,
|
| 111 |
+
outputs=output
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Allow Enter key to generate
|
| 115 |
+
prompt_input.submit(
|
| 116 |
+
fn=generate_code,
|
| 117 |
+
inputs=prompt_input,
|
| 118 |
+
outputs=output
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
return interface
|
| 122 |
|
| 123 |
+
# Create and launch interface
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
interface = create_interface()
|
| 126 |
+
interface.launch(
|
| 127 |
+
server_name="0.0.0.0",
|
| 128 |
+
server_port=7860,
|
| 129 |
+
share=True,
|
| 130 |
+
show_error=True
|
| 131 |
+
)
|
requirements.txt
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
transformers
|
| 2 |
torch
|
| 3 |
accelerate
|
|
|
|
| 4 |
fastapi
|
| 5 |
uvicorn
|
|
|
|
| 1 |
transformers
|
| 2 |
torch
|
| 3 |
accelerate
|
| 4 |
+
gradio
|
| 5 |
fastapi
|
| 6 |
uvicorn
|