Spaces:
Runtime error
Runtime error
Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -1,74 +1,139 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
import time
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def simulate_processing(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
Simulates a Review, Revise, Refactor, and Iterate workflow.
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
if not content.strip():
|
| 11 |
-
return "", 0, "β οΈ Please enter some text or code to begin
|
| 12 |
|
| 13 |
-
# Simulate processing time
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
result = content
|
| 17 |
status_msg = ""
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
if operation == "Review":
|
| 20 |
-
status_msg = "
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
elif operation == "Revise":
|
| 24 |
-
status_msg = "
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
sentences = content.split('. ')
|
| 27 |
-
revised = '. '.join([s.strip().capitalize() for s in sentences])
|
| 28 |
result = revised if revised else content
|
| 29 |
-
|
|
|
|
| 30 |
elif operation == "Refactor":
|
| 31 |
-
status_msg = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Simple code simulation
|
| 33 |
result = content.replace("var ", "let ").replace(" ", " ")
|
| 34 |
-
result = f"# REFACTORED VERSION\n{result}"
|
| 35 |
-
|
|
|
|
| 36 |
elif operation == "Iterate":
|
| 37 |
iteration += 1
|
| 38 |
-
status_msg = f"
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
return result, iteration, status_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Gradio 6 Application
|
| 44 |
-
#
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
|
| 47 |
-
# Header
|
| 48 |
gr.HTML("""
|
| 49 |
-
<div style="text-align: center; margin-bottom:
|
| 50 |
-
<h1
|
| 51 |
-
<p style="color: #
|
| 52 |
-
<
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
</div>
|
| 56 |
""")
|
| 57 |
|
| 58 |
-
with gr.Sidebar(position="left"):
|
| 59 |
-
gr.Markdown("### π οΈ
|
| 60 |
-
|
| 61 |
-
operation_mode = gr.Radio(
|
| 62 |
-
choices=["Review", "Revise", "Refactor", "Iterate"],
|
| 63 |
-
value="Review",
|
| 64 |
-
label="Select Operation",
|
| 65 |
-
info="Choose the action to perform on the input."
|
| 66 |
-
)
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
gr.Markdown("---")
|
| 71 |
-
gr.Markdown("###
|
| 72 |
gr.Markdown("""
|
| 73 |
1. **Review**: Analyze syntax and structure.
|
| 74 |
2. **Revise**: Improve phrasing and tone.
|
|
@@ -79,58 +144,103 @@ with gr.Blocks() as demo:
|
|
| 79 |
with gr.Row():
|
| 80 |
with gr.Column(scale=1):
|
| 81 |
input_code = gr.Code(
|
| 82 |
-
label="Input
|
| 83 |
language="python",
|
| 84 |
placeholder="Paste your code or text here...",
|
| 85 |
-
lines=
|
|
|
|
| 86 |
)
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
-
clear_btn = gr.Button("Clear", variant="secondary", scale=1)
|
| 89 |
-
run_btn = gr.Button("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
with gr.Column(scale=1):
|
| 92 |
-
status_output = gr.
|
|
|
|
| 93 |
output_code = gr.Code(
|
| 94 |
-
label="
|
| 95 |
language="python",
|
| 96 |
-
lines=
|
| 97 |
interactive=True
|
| 98 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
# Event Listeners
|
| 101 |
-
# Gradio 6 uses api_visibility instead of api_name for visibility control
|
| 102 |
run_btn.click(
|
| 103 |
-
fn=
|
| 104 |
-
inputs=[input_code, operation_mode, iteration_state],
|
| 105 |
-
outputs=[output_code, iteration_state, status_output],
|
| 106 |
api_visibility="public"
|
| 107 |
)
|
| 108 |
|
| 109 |
clear_btn.click(
|
| 110 |
-
fn=lambda:
|
| 111 |
inputs=None,
|
| 112 |
-
outputs=[input_code, iteration_state,
|
| 113 |
api_visibility="private"
|
| 114 |
)
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# Gradio 6 Launch Configuration
|
| 117 |
-
# ALL app-level parameters (theme, css, etc.) go here
|
| 118 |
demo.launch(
|
| 119 |
theme=gr.themes.Soft(
|
| 120 |
-
primary_hue="
|
| 121 |
-
secondary_hue="
|
| 122 |
neutral_hue="slate",
|
| 123 |
font=gr.themes.GoogleFont("Inter"),
|
| 124 |
text_size="md",
|
| 125 |
-
spacing_size="
|
| 126 |
-
radius_size="
|
| 127 |
).set(
|
| 128 |
-
button_primary_background_fill="*
|
| 129 |
-
button_primary_background_fill_hover="*
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
),
|
| 132 |
footer_links=[
|
| 133 |
-
{"label": "
|
|
|
|
| 134 |
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 135 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
import time
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
def simulate_processing(
|
| 9 |
+
content: str,
|
| 10 |
+
operation: str,
|
| 11 |
+
iteration: int,
|
| 12 |
+
creativity_level: float
|
| 13 |
+
) -> tuple[str, int, str, str | None]:
|
| 14 |
"""
|
| 15 |
Simulates a Review, Revise, Refactor, and Iterate workflow.
|
| 16 |
+
Includes simulated 'AI' parameters for a studio feel.
|
| 17 |
"""
|
| 18 |
if not content.strip():
|
| 19 |
+
return "", 0, "β οΈ **Please enter some text or code to begin.**", None
|
| 20 |
|
| 21 |
+
# Simulate processing time based on creativity level
|
| 22 |
+
delay = 0.3 + (creativity_level * 0.5)
|
| 23 |
+
time.sleep(delay)
|
| 24 |
|
| 25 |
result = content
|
| 26 |
status_msg = ""
|
| 27 |
+
download_filename = None
|
| 28 |
|
| 29 |
+
# Generate a timestamp for logs
|
| 30 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 31 |
+
|
| 32 |
if operation == "Review":
|
| 33 |
+
status_msg = f"""
|
| 34 |
+
### β
Review Complete
|
| 35 |
+
- **Status:** Pass
|
| 36 |
+
- **Time:** {timestamp}
|
| 37 |
+
- **Analysis:** Syntax looks valid.
|
| 38 |
+
- **Suggestion:** Consider adding docstrings and type hints for better maintainability.
|
| 39 |
+
"""
|
| 40 |
+
result = f"# REVIEW LOG [{timestamp}]\n# Status: Pass\n# Note: Code structure is solid.\n\n{content}"
|
| 41 |
+
download_filename = "review_log.py"
|
| 42 |
+
|
| 43 |
elif operation == "Revise":
|
| 44 |
+
status_msg = """
|
| 45 |
+
### βοΈ Revision Complete
|
| 46 |
+
- **Tone:** Professional
|
| 47 |
+
- **Grammar:** Corrected
|
| 48 |
+
- **Flow:** Optimized
|
| 49 |
+
"""
|
| 50 |
+
# Simple text simulation: capitalize and clean up
|
| 51 |
sentences = content.split('. ')
|
| 52 |
+
revised = '. '.join([s.strip().capitalize() for s in sentences if s.strip()])
|
| 53 |
result = revised if revised else content
|
| 54 |
+
download_filename = "revised_text.txt"
|
| 55 |
+
|
| 56 |
elif operation == "Refactor":
|
| 57 |
+
status_msg = """
|
| 58 |
+
### βοΈ Refactoring Complete
|
| 59 |
+
- **Optimization:** Applied
|
| 60 |
+
- **Complexity:** Reduced
|
| 61 |
+
- **Style:** PEP8 Compliant
|
| 62 |
+
"""
|
| 63 |
# Simple code simulation
|
| 64 |
result = content.replace("var ", "let ").replace(" ", " ")
|
| 65 |
+
result = f"# REFACTORED VERSION [{timestamp}]\n# Optimized for performance\n{result}"
|
| 66 |
+
download_filename = "refactored_code.py"
|
| 67 |
+
|
| 68 |
elif operation == "Iterate":
|
| 69 |
iteration += 1
|
| 70 |
+
status_msg = f"""
|
| 71 |
+
### π Iteration {iteration} Generated
|
| 72 |
+
- **Creativity Level:** {creativity_level:.2f}
|
| 73 |
+
- **Action:** Created new version based on feedback.
|
| 74 |
+
"""
|
| 75 |
+
result = f"# --- ITERATION {iteration} ---\n# Generated at {timestamp}\n# Creativity: {creativity_level}\n{content}"
|
| 76 |
+
download_filename = f"iteration_{iteration}.py"
|
| 77 |
+
|
| 78 |
+
# Create a temporary file for download
|
| 79 |
+
temp_file = None
|
| 80 |
+
if download_filename and result:
|
| 81 |
+
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".py") as f:
|
| 82 |
+
f.write(result)
|
| 83 |
+
temp_file = f.name
|
| 84 |
|
| 85 |
+
return result, iteration, status_msg, temp_file
|
| 86 |
+
|
| 87 |
+
def create_download_file(content: str) -> str:
|
| 88 |
+
"""Helper to create a downloadable file object."""
|
| 89 |
+
if not content:
|
| 90 |
+
return None
|
| 91 |
+
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
|
| 92 |
+
f.write(content)
|
| 93 |
+
return f.name
|
| 94 |
|
| 95 |
# Gradio 6 Application
|
| 96 |
+
# NO parameters in gr.Blocks() constructor!
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
|
| 99 |
+
# Professional Header
|
| 100 |
gr.HTML("""
|
| 101 |
+
<div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #f0fdf4 0%, #ecfeff 100%); border-radius: 12px; margin-bottom: 25px; border: 1px solid #ccfbf1;">
|
| 102 |
+
<h1 style="margin: 0; color: #0f172a; font-family: 'Inter', sans-serif;">π Studio Workflow Engine</h1>
|
| 103 |
+
<p style="margin: 10px 0 0 0; color: #64748b; font-size: 1.1em;">Professional Review, Revise, Refactor, and Iteration Tool</p>
|
| 104 |
+
<div style="margin-top: 15px;">
|
| 105 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank"
|
| 106 |
+
style="background-color: #10b981; color: white; padding: 8px 16px; border-radius: 6px; text-decoration: none; font-weight: 600; font-size: 0.9em; box-shadow: 0 2px 4px rgba(16, 185, 129, 0.2);">
|
| 107 |
+
Built with anycoder
|
| 108 |
+
</a>
|
| 109 |
+
</div>
|
| 110 |
</div>
|
| 111 |
""")
|
| 112 |
|
| 113 |
+
with gr.Sidebar(position="left", width=320):
|
| 114 |
+
gr.Markdown("### π οΈ Control Panel")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
with gr.Group():
|
| 117 |
+
operation_mode = gr.Radio(
|
| 118 |
+
choices=["Review", "Revise", "Refactor", "Iterate"],
|
| 119 |
+
value="Review",
|
| 120 |
+
label="Operation Mode",
|
| 121 |
+
info="Select the workflow stage."
|
| 122 |
+
)
|
| 123 |
|
| 124 |
+
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 125 |
+
creativity = gr.Slider(
|
| 126 |
+
minimum=0.0,
|
| 127 |
+
maximum=1.0,
|
| 128 |
+
value=0.5,
|
| 129 |
+
step=0.1,
|
| 130 |
+
label="Creativity Level",
|
| 131 |
+
info="Simulates AI randomness"
|
| 132 |
+
)
|
| 133 |
+
iteration_state = gr.Number(value=0, label="Current Iteration", interactive=False)
|
| 134 |
+
|
| 135 |
gr.Markdown("---")
|
| 136 |
+
gr.Markdown("### π Workflow Guide")
|
| 137 |
gr.Markdown("""
|
| 138 |
1. **Review**: Analyze syntax and structure.
|
| 139 |
2. **Revise**: Improve phrasing and tone.
|
|
|
|
| 144 |
with gr.Row():
|
| 145 |
with gr.Column(scale=1):
|
| 146 |
input_code = gr.Code(
|
| 147 |
+
label="Input Source",
|
| 148 |
language="python",
|
| 149 |
placeholder="Paste your code or text here...",
|
| 150 |
+
lines=20,
|
| 151 |
+
show_label=True
|
| 152 |
)
|
| 153 |
+
|
| 154 |
with gr.Row():
|
| 155 |
+
clear_btn = gr.Button("Clear Workspace", variant="secondary", scale=1, size="lg")
|
| 156 |
+
run_btn = gr.Button("Execute Workflow", variant="primary", scale=2, size="lg")
|
| 157 |
+
|
| 158 |
+
# Examples Section
|
| 159 |
+
gr.Markdown("### π‘ Quick Start Examples")
|
| 160 |
+
gr.Examples(
|
| 161 |
+
examples=[
|
| 162 |
+
["def hello():\n print('Hello World')", "Review"],
|
| 163 |
+
["this is a poorly written sentence. it needs help.", "Revise"],
|
| 164 |
+
["var x = 10;\nvar y = 20;\nreturn x + y;", "Refactor"],
|
| 165 |
+
],
|
| 166 |
+
inputs=[input_code, operation_mode],
|
| 167 |
+
label="Try these samples:"
|
| 168 |
+
)
|
| 169 |
|
| 170 |
with gr.Column(scale=1):
|
| 171 |
+
status_output = gr.Markdown(label="Process Log", value="*Ready to process...*")
|
| 172 |
+
|
| 173 |
output_code = gr.Code(
|
| 174 |
+
label="Result Output",
|
| 175 |
language="python",
|
| 176 |
+
lines=18,
|
| 177 |
interactive=True
|
| 178 |
)
|
| 179 |
+
|
| 180 |
+
download_btn = gr.DownloadButton(
|
| 181 |
+
label="π₯ Download Result",
|
| 182 |
+
variant="secondary",
|
| 183 |
+
visible=False
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
# Event Listeners using Gradio 6 syntax
|
| 187 |
+
def process_wrapper(content, op, iter_count, creat):
|
| 188 |
+
res, new_iter, status, temp_path = simulate_processing(content, op, iter_count, creat)
|
| 189 |
+
return res, new_iter, status, gr.DownloadButton(visible=True, value=temp_path)
|
| 190 |
|
|
|
|
|
|
|
| 191 |
run_btn.click(
|
| 192 |
+
fn=process_wrapper,
|
| 193 |
+
inputs=[input_code, operation_mode, iteration_state, creativity],
|
| 194 |
+
outputs=[output_code, iteration_state, status_output, download_btn],
|
| 195 |
api_visibility="public"
|
| 196 |
)
|
| 197 |
|
| 198 |
clear_btn.click(
|
| 199 |
+
fn=lambda: ("", 0, "*Ready to process...*", gr.DownloadButton(visible=False, value=None), "Review", 0.5),
|
| 200 |
inputs=None,
|
| 201 |
+
outputs=[input_code, iteration_state, status_output, download_btn, operation_mode, creativity],
|
| 202 |
api_visibility="private"
|
| 203 |
)
|
| 204 |
|
| 205 |
+
# Allow downloading directly from the output code box if modified manually
|
| 206 |
+
output_code.change(
|
| 207 |
+
fn=lambda x: gr.DownloadButton(visible=True, value=create_download_file(x)) if x else gr.DownloadButton(visible=False),
|
| 208 |
+
inputs=[output_code],
|
| 209 |
+
outputs=[download_btn]
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
# Gradio 6 Launch Configuration
|
| 213 |
+
# ALL app-level parameters (theme, css, etc.) go here!
|
| 214 |
demo.launch(
|
| 215 |
theme=gr.themes.Soft(
|
| 216 |
+
primary_hue="emerald",
|
| 217 |
+
secondary_hue="teal",
|
| 218 |
neutral_hue="slate",
|
| 219 |
font=gr.themes.GoogleFont("Inter"),
|
| 220 |
text_size="md",
|
| 221 |
+
spacing_size="lg",
|
| 222 |
+
radius_size="md"
|
| 223 |
).set(
|
| 224 |
+
button_primary_background_fill="*primary_600",
|
| 225 |
+
button_primary_background_fill_hover="*primary_700",
|
| 226 |
+
button_primary_text_color="white",
|
| 227 |
+
button_secondary_background_fill="*neutral_100",
|
| 228 |
+
block_border_width="1px",
|
| 229 |
+
block_border_color="*neutral_200",
|
| 230 |
+
body_background_fill="*background_fill_primary",
|
| 231 |
+
shadow_drop="lg",
|
| 232 |
),
|
| 233 |
footer_links=[
|
| 234 |
+
{"label": "Documentation", "url": "https://www.gradio.app/docs"},
|
| 235 |
+
{"label": "GitHub", "url": "https://github.com/gradio-app/gradio"},
|
| 236 |
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 237 |
+
],
|
| 238 |
+
# Add a custom footer message
|
| 239 |
+
head="""
|
| 240 |
+
<style>
|
| 241 |
+
.gradio-container {
|
| 242 |
+
max-width: 1400px !important;
|
| 243 |
+
}
|
| 244 |
+
</style>
|
| 245 |
+
"""
|
| 246 |
)
|