Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,26 +39,23 @@ def generate_code(prompt, task_type, language, max_tokens, temperature):
|
|
| 39 |
else: # Explain Code
|
| 40 |
system_prompt = f"You are an expert {language} teacher. Explain the following code step by step:"
|
| 41 |
full_prompt = f"{system_prompt}\n\n{prompt}\n\nExplanation:"
|
| 42 |
-
|
| 43 |
-
messages = [
|
| 44 |
-
{"role": "user", "content": full_prompt},
|
| 45 |
-
]
|
| 46 |
-
|
| 47 |
outputs = pipe(
|
| 48 |
-
|
| 49 |
max_new_tokens=int(max_tokens),
|
| 50 |
temperature=temperature,
|
| 51 |
do_sample=True,
|
| 52 |
pad_token_id=pipe.tokenizer.eos_token_id
|
| 53 |
)
|
| 54 |
|
| 55 |
-
generated_text = outputs[0]["generated_text"]
|
| 56 |
|
| 57 |
# Extract code if it's wrapped in code blocks
|
| 58 |
code_match = re.search(r'```(?:\w+\n)?(.*?)```', generated_text, re.DOTALL)
|
| 59 |
if code_match:
|
| 60 |
code_output = code_match.group(1).strip()
|
| 61 |
else:
|
|
|
|
| 62 |
code_output = generated_text.strip()
|
| 63 |
|
| 64 |
# Generate explanation based on the output
|
|
@@ -80,7 +77,6 @@ css = """
|
|
| 80 |
.gradio-container {
|
| 81 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 82 |
}
|
| 83 |
-
|
| 84 |
.header {
|
| 85 |
text-align: center;
|
| 86 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
@@ -90,20 +86,17 @@ css = """
|
|
| 90 |
margin-bottom: 2rem;
|
| 91 |
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
| 92 |
}
|
| 93 |
-
|
| 94 |
.header h1 {
|
| 95 |
font-size: 2.5rem;
|
| 96 |
font-weight: 700;
|
| 97 |
margin: 0;
|
| 98 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 99 |
}
|
| 100 |
-
|
| 101 |
.header p {
|
| 102 |
font-size: 1.2rem;
|
| 103 |
margin: 0.5rem 0 0 0;
|
| 104 |
opacity: 0.9;
|
| 105 |
}
|
| 106 |
-
|
| 107 |
.custom-button {
|
| 108 |
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
| 109 |
border: none;
|
|
@@ -112,12 +105,10 @@ css = """
|
|
| 112 |
border-radius: 8px;
|
| 113 |
transition: all 0.3s ease;
|
| 114 |
}
|
| 115 |
-
|
| 116 |
.custom-button:hover {
|
| 117 |
transform: translateY(-2px);
|
| 118 |
box-shadow: 0 5px 15px rgba(79, 172, 254, 0.4);
|
| 119 |
}
|
| 120 |
-
|
| 121 |
.footer {
|
| 122 |
text-align: center;
|
| 123 |
margin-top: 3rem;
|
|
@@ -127,12 +118,10 @@ css = """
|
|
| 127 |
border-radius: 15px;
|
| 128 |
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
|
| 129 |
}
|
| 130 |
-
|
| 131 |
.footer h3 {
|
| 132 |
margin: 0 0 1rem 0;
|
| 133 |
font-size: 1.3rem;
|
| 134 |
}
|
| 135 |
-
|
| 136 |
.footer a {
|
| 137 |
color: #74b9ff;
|
| 138 |
text-decoration: none;
|
|
@@ -140,24 +129,20 @@ css = """
|
|
| 140 |
font-weight: 500;
|
| 141 |
transition: color 0.3s ease;
|
| 142 |
}
|
| 143 |
-
|
| 144 |
.footer a:hover {
|
| 145 |
color: #0984e3;
|
| 146 |
}
|
| 147 |
-
|
| 148 |
.status-box {
|
| 149 |
padding: 1rem;
|
| 150 |
border-radius: 8px;
|
| 151 |
margin: 1rem 0;
|
| 152 |
font-weight: 500;
|
| 153 |
}
|
| 154 |
-
|
| 155 |
.code-output {
|
| 156 |
background: #1e1e1e;
|
| 157 |
border-radius: 8px;
|
| 158 |
border: 1px solid #333;
|
| 159 |
}
|
| 160 |
-
|
| 161 |
.explanation-output {
|
| 162 |
background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
|
| 163 |
border-radius: 8px;
|
|
@@ -298,15 +283,15 @@ with gr.Blocks(css=css, title="AI Code Generator & Bug Fixer", theme=gr.themes.S
|
|
| 298 |
outputs=[code_output, explanation_output]
|
| 299 |
)
|
| 300 |
|
| 301 |
-
#
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
language.change(
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
)
|
| 310 |
|
| 311 |
# Launch the app
|
| 312 |
if __name__ == "__main__":
|
|
@@ -315,4 +300,4 @@ if __name__ == "__main__":
|
|
| 315 |
server_port=7860,
|
| 316 |
share=True,
|
| 317 |
debug=True
|
| 318 |
-
)
|
|
|
|
| 39 |
else: # Explain Code
|
| 40 |
system_prompt = f"You are an expert {language} teacher. Explain the following code step by step:"
|
| 41 |
full_prompt = f"{system_prompt}\n\n{prompt}\n\nExplanation:"
|
| 42 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
outputs = pipe(
|
| 44 |
+
full_prompt,
|
| 45 |
max_new_tokens=int(max_tokens),
|
| 46 |
temperature=temperature,
|
| 47 |
do_sample=True,
|
| 48 |
pad_token_id=pipe.tokenizer.eos_token_id
|
| 49 |
)
|
| 50 |
|
| 51 |
+
generated_text = outputs[0]["generated_text"]
|
| 52 |
|
| 53 |
# Extract code if it's wrapped in code blocks
|
| 54 |
code_match = re.search(r'```(?:\w+\n)?(.*?)```', generated_text, re.DOTALL)
|
| 55 |
if code_match:
|
| 56 |
code_output = code_match.group(1).strip()
|
| 57 |
else:
|
| 58 |
+
# For "Explain Code" task, code_output should hold explanation text instead
|
| 59 |
code_output = generated_text.strip()
|
| 60 |
|
| 61 |
# Generate explanation based on the output
|
|
|
|
| 77 |
.gradio-container {
|
| 78 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 79 |
}
|
|
|
|
| 80 |
.header {
|
| 81 |
text-align: center;
|
| 82 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
|
|
| 86 |
margin-bottom: 2rem;
|
| 87 |
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
| 88 |
}
|
|
|
|
| 89 |
.header h1 {
|
| 90 |
font-size: 2.5rem;
|
| 91 |
font-weight: 700;
|
| 92 |
margin: 0;
|
| 93 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 94 |
}
|
|
|
|
| 95 |
.header p {
|
| 96 |
font-size: 1.2rem;
|
| 97 |
margin: 0.5rem 0 0 0;
|
| 98 |
opacity: 0.9;
|
| 99 |
}
|
|
|
|
| 100 |
.custom-button {
|
| 101 |
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
| 102 |
border: none;
|
|
|
|
| 105 |
border-radius: 8px;
|
| 106 |
transition: all 0.3s ease;
|
| 107 |
}
|
|
|
|
| 108 |
.custom-button:hover {
|
| 109 |
transform: translateY(-2px);
|
| 110 |
box-shadow: 0 5px 15px rgba(79, 172, 254, 0.4);
|
| 111 |
}
|
|
|
|
| 112 |
.footer {
|
| 113 |
text-align: center;
|
| 114 |
margin-top: 3rem;
|
|
|
|
| 118 |
border-radius: 15px;
|
| 119 |
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
|
| 120 |
}
|
|
|
|
| 121 |
.footer h3 {
|
| 122 |
margin: 0 0 1rem 0;
|
| 123 |
font-size: 1.3rem;
|
| 124 |
}
|
|
|
|
| 125 |
.footer a {
|
| 126 |
color: #74b9ff;
|
| 127 |
text-decoration: none;
|
|
|
|
| 129 |
font-weight: 500;
|
| 130 |
transition: color 0.3s ease;
|
| 131 |
}
|
|
|
|
| 132 |
.footer a:hover {
|
| 133 |
color: #0984e3;
|
| 134 |
}
|
|
|
|
| 135 |
.status-box {
|
| 136 |
padding: 1rem;
|
| 137 |
border-radius: 8px;
|
| 138 |
margin: 1rem 0;
|
| 139 |
font-weight: 500;
|
| 140 |
}
|
|
|
|
| 141 |
.code-output {
|
| 142 |
background: #1e1e1e;
|
| 143 |
border-radius: 8px;
|
| 144 |
border: 1px solid #333;
|
| 145 |
}
|
|
|
|
| 146 |
.explanation-output {
|
| 147 |
background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
|
| 148 |
border-radius: 8px;
|
|
|
|
| 283 |
outputs=[code_output, explanation_output]
|
| 284 |
)
|
| 285 |
|
| 286 |
+
# Note: Dynamic update of the code block's language is tricky in Gradio.
|
| 287 |
+
# This is commented out as it won't update the existing gr.Code component properly.
|
| 288 |
+
# def update_code_language(lang):
|
| 289 |
+
# return lang.lower()
|
| 290 |
+
# language.change(
|
| 291 |
+
# fn=update_code_language,
|
| 292 |
+
# inputs=language,
|
| 293 |
+
# outputs=code_output.language
|
| 294 |
+
# )
|
| 295 |
|
| 296 |
# Launch the app
|
| 297 |
if __name__ == "__main__":
|
|
|
|
| 300 |
server_port=7860,
|
| 301 |
share=True,
|
| 302 |
debug=True
|
| 303 |
+
)
|