Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
@@ -24,11 +24,11 @@ if os.path.exists(CACHE_FILE):
|
|
| 24 |
cache = json.load(f)
|
| 25 |
|
| 26 |
def code_assistant(prompt, language):
|
| 27 |
-
# Input validation
|
| 28 |
if not prompt.strip():
|
| 29 |
-
return "Error: The input prompt cannot be empty. Please provide a coding question or code snippet."
|
| 30 |
-
if len(prompt) >
|
| 31 |
-
return "Error: The input prompt is too long. Please limit it to
|
| 32 |
|
| 33 |
# Check if the prompt is in cache
|
| 34 |
cache_key = (prompt, language)
|
|
@@ -45,7 +45,7 @@ def code_assistant(prompt, language):
|
|
| 45 |
# Generate response with adjusted parameters for faster CPU response
|
| 46 |
outputs = model.generate(
|
| 47 |
inputs.input_ids,
|
| 48 |
-
max_length=
|
| 49 |
temperature=0.1, # Lower temperature for focused output
|
| 50 |
top_p=0.8, # Slightly reduced top_p for quicker sampling
|
| 51 |
do_sample=True
|
|
@@ -65,16 +65,79 @@ def code_assistant(prompt, language):
|
|
| 65 |
|
| 66 |
return generated_text
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Set up Gradio interface with a dropdown for programming language selection
|
| 69 |
iface = gr.Interface(
|
| 70 |
fn=code_assistant,
|
| 71 |
inputs=[
|
| 72 |
-
gr.Textbox(lines=5, placeholder="Ask a coding question or paste your code here..."),
|
| 73 |
gr.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "HTML", "CSS", "SQL", "Other"], label="Programming Language")
|
| 74 |
],
|
| 75 |
outputs="text",
|
| 76 |
-
title=
|
| 77 |
-
description=
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
# Launch the Gradio app
|
|
|
|
| 1 |
+
# app.py (with 1024-character limit)
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 24 |
cache = json.load(f)
|
| 25 |
|
| 26 |
def code_assistant(prompt, language):
|
| 27 |
+
# Input validation with a 1024-character limit
|
| 28 |
if not prompt.strip():
|
| 29 |
+
return "⚠️ Error: The input prompt cannot be empty. Please provide a coding question or code snippet."
|
| 30 |
+
if len(prompt) > 1024:
|
| 31 |
+
return "⚠️ Error: The input prompt is too long. Please limit it to 1024 characters."
|
| 32 |
|
| 33 |
# Check if the prompt is in cache
|
| 34 |
cache_key = (prompt, language)
|
|
|
|
| 45 |
# Generate response with adjusted parameters for faster CPU response
|
| 46 |
outputs = model.generate(
|
| 47 |
inputs.input_ids,
|
| 48 |
+
max_length=256, # Shortened max length for quicker response
|
| 49 |
temperature=0.1, # Lower temperature for focused output
|
| 50 |
top_p=0.8, # Slightly reduced top_p for quicker sampling
|
| 51 |
do_sample=True
|
|
|
|
| 65 |
|
| 66 |
return generated_text
|
| 67 |
|
| 68 |
+
# Custom CSS styling for animations and colors
|
| 69 |
+
css = """
|
| 70 |
+
/* Center-align all text in the input and output boxes */
|
| 71 |
+
input, textarea, .output_text {
|
| 72 |
+
text-align: center;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
/* Style the main title */
|
| 76 |
+
h1 {
|
| 77 |
+
color: #1e90ff;
|
| 78 |
+
font-family: 'Arial', sans-serif;
|
| 79 |
+
text-align: center;
|
| 80 |
+
font-weight: bold;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
/* Style the description */
|
| 84 |
+
.description {
|
| 85 |
+
color: #555;
|
| 86 |
+
font-family: 'Arial', sans-serif;
|
| 87 |
+
text-align: center;
|
| 88 |
+
margin-bottom: 20px;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
/* Output box animation */
|
| 92 |
+
.output_text {
|
| 93 |
+
color: #1e90ff;
|
| 94 |
+
animation: fadeIn 2s ease-in-out;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
/* Add fade-in animation */
|
| 98 |
+
@keyframes fadeIn {
|
| 99 |
+
0% { opacity: 0; }
|
| 100 |
+
100% { opacity: 1; }
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
/* Hover effect for the submit button */
|
| 104 |
+
button {
|
| 105 |
+
background-color: #1e90ff;
|
| 106 |
+
color: white;
|
| 107 |
+
font-weight: bold;
|
| 108 |
+
border: none;
|
| 109 |
+
padding: 10px 20px;
|
| 110 |
+
border-radius: 5px;
|
| 111 |
+
transition: background-color 0.3s ease;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
button:hover {
|
| 115 |
+
background-color: #104e8b;
|
| 116 |
+
cursor: pointer;
|
| 117 |
+
}
|
| 118 |
+
"""
|
| 119 |
+
|
| 120 |
+
# Enhanced title and description with HTML styling
|
| 121 |
+
title_html = """
|
| 122 |
+
<h1>💻 CodeBand: AI Code Assistant</h1>
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
description_html = """
|
| 126 |
+
<p class="description">An AI-powered assistant for coding queries, debugging, and code generation.
|
| 127 |
+
Choose a programming language for more tailored responses. Limited to 1024 characters.</p>
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
# Set up Gradio interface with a dropdown for programming language selection
|
| 131 |
iface = gr.Interface(
|
| 132 |
fn=code_assistant,
|
| 133 |
inputs=[
|
| 134 |
+
gr.Textbox(lines=5, placeholder="Ask a coding question or paste your code here...", max_chars=1024),
|
| 135 |
gr.Dropdown(choices=["Python", "JavaScript", "Java", "C++", "HTML", "CSS", "SQL", "Other"], label="Programming Language")
|
| 136 |
],
|
| 137 |
outputs="text",
|
| 138 |
+
title=title_html,
|
| 139 |
+
description=description_html,
|
| 140 |
+
css=css # Add custom CSS
|
| 141 |
)
|
| 142 |
|
| 143 |
# Launch the Gradio app
|