Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,8 @@
|
|
1 |
-
from flask import Flask, send_file
|
2 |
import gradio as gr
|
3 |
import ast
|
4 |
import os
|
5 |
from io import StringIO
|
6 |
|
7 |
-
app = Flask(__name__)
|
8 |
-
|
9 |
# Mapping of Gradio components to Toga equivalents
|
10 |
GRADIO_TO_TOGA = {
|
11 |
"Textbox": ("toga.TextInput", "style=Pack(padding=5)", "Text Input"),
|
@@ -24,10 +21,8 @@ def parse_gradio_code(code):
|
|
24 |
functions = []
|
25 |
|
26 |
for node in ast.walk(tree):
|
27 |
-
# Extract function definitions
|
28 |
if isinstance(node, ast.FunctionDef):
|
29 |
functions.append(node.name)
|
30 |
-
# Extract Gradio component calls
|
31 |
if isinstance(node, ast.Call) and hasattr(node.func, 'attr'):
|
32 |
if node.func.attr in GRADIO_TO_TOGA:
|
33 |
component = node.func.attr
|
@@ -57,7 +52,6 @@ def generate_toga_code(components, functions):
|
|
57 |
for idx, comp in enumerate(components):
|
58 |
toga_comp, extra, label_prefix = GRADIO_TO_TOGA.get(comp["type"], ("toga.Label", f"Placeholder: {comp['type']} not supported", "Unknown"))
|
59 |
|
60 |
-
# Handle component-specific logic
|
61 |
if comp["type"] == "Textbox":
|
62 |
label = comp["kwargs"].get("label", f"'{label_prefix} {idx}'")
|
63 |
toga_code.append(f" label_{idx} = toga.Label({label}, style=Pack(padding=(5, 5, 0, 5)))")
|
@@ -114,78 +108,39 @@ def generate_toga_code(components, functions):
|
|
114 |
# Conversion function
|
115 |
def convert_gradio_to_toga(file):
|
116 |
if not file:
|
117 |
-
return "Please upload a Gradio app Python file."
|
118 |
|
119 |
try:
|
120 |
-
# Read uploaded file
|
121 |
code = file.decode('utf-8')
|
122 |
components, functions = parse_gradio_code(code)
|
123 |
|
124 |
if not components and functions and isinstance(functions[0], str):
|
125 |
-
return f"Error: {functions[0]}"
|
126 |
|
127 |
-
# Generate Toga code
|
128 |
toga_code = generate_toga_code(components, functions)
|
129 |
-
|
130 |
-
# Save to temporary file
|
131 |
output_path = "/tmp/toga_app.py"
|
132 |
with open(output_path, "w") as f:
|
133 |
f.write(toga_code)
|
134 |
|
135 |
-
return output_path
|
136 |
except Exception as e:
|
137 |
-
return f"Error: {str(e)}"
|
138 |
|
139 |
# Gradio interface
|
140 |
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
141 |
gr.Markdown("# Gradio to Toga Converter")
|
142 |
-
gr.Markdown("Upload a Gradio app
|
143 |
-
file_input = gr.File(label="Upload Gradio App
|
144 |
convert_button = gr.Button("Convert to Toga")
|
145 |
-
output = gr.File(label="Download Toga App
|
146 |
-
|
147 |
|
148 |
convert_button.click(
|
149 |
fn=convert_gradio_to_toga,
|
150 |
inputs=file_input,
|
151 |
-
outputs=[output,
|
152 |
)
|
153 |
|
154 |
-
#
|
155 |
-
@app.route('/')
|
156 |
-
def serve_gradio():
|
157 |
-
# For Hugging Face Spaces, use environment port
|
158 |
-
port = int(os.environ.get("PORT", 7860))
|
159 |
-
interface.launch(server_name="0.0.0.0", server_port=port, quiet=True)
|
160 |
-
return """
|
161 |
-
<!DOCTYPE html>
|
162 |
-
<html>
|
163 |
-
<head>
|
164 |
-
<title>Gradio to Toga Converter</title>
|
165 |
-
<style>
|
166 |
-
body { font-family: Arial, sans-serif; text-align: center; padding: 20px; }
|
167 |
-
h1 { color: #333; }
|
168 |
-
p { color: #666; }
|
169 |
-
</style>
|
170 |
-
</head>
|
171 |
-
<body>
|
172 |
-
<h1>Gradio to Toga Converter</h1>
|
173 |
-
<p>The converter is running. Use the Gradio interface to upload your Gradio app and download the Toga version.</p>
|
174 |
-
</body>
|
175 |
-
</html>
|
176 |
-
"""
|
177 |
-
|
178 |
-
@app.route('/convert', methods=['POST'])
|
179 |
-
def convert():
|
180 |
-
file = request.files.get('file')
|
181 |
-
if not file:
|
182 |
-
return {"error": "No file uploaded"}, 400
|
183 |
-
|
184 |
-
result = convert_gradio_to_toga(file.read())
|
185 |
-
if result.startswith("Error"):
|
186 |
-
return {"error": result}, 400
|
187 |
-
|
188 |
-
return send_file(result, as_attachment=True, download_name="toga_app.py")
|
189 |
-
|
190 |
if __name__ == "__main__":
|
191 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import ast
|
3 |
import os
|
4 |
from io import StringIO
|
5 |
|
|
|
|
|
6 |
# Mapping of Gradio components to Toga equivalents
|
7 |
GRADIO_TO_TOGA = {
|
8 |
"Textbox": ("toga.TextInput", "style=Pack(padding=5)", "Text Input"),
|
|
|
21 |
functions = []
|
22 |
|
23 |
for node in ast.walk(tree):
|
|
|
24 |
if isinstance(node, ast.FunctionDef):
|
25 |
functions.append(node.name)
|
|
|
26 |
if isinstance(node, ast.Call) and hasattr(node.func, 'attr'):
|
27 |
if node.func.attr in GRADIO_TO_TOGA:
|
28 |
component = node.func.attr
|
|
|
52 |
for idx, comp in enumerate(components):
|
53 |
toga_comp, extra, label_prefix = GRADIO_TO_TOGA.get(comp["type"], ("toga.Label", f"Placeholder: {comp['type']} not supported", "Unknown"))
|
54 |
|
|
|
55 |
if comp["type"] == "Textbox":
|
56 |
label = comp["kwargs"].get("label", f"'{label_prefix} {idx}'")
|
57 |
toga_code.append(f" label_{idx} = toga.Label({label}, style=Pack(padding=(5, 5, 0, 5)))")
|
|
|
108 |
# Conversion function
|
109 |
def convert_gradio_to_toga(file):
|
110 |
if not file:
|
111 |
+
return None, "Please upload a Gradio app Python file."
|
112 |
|
113 |
try:
|
|
|
114 |
code = file.decode('utf-8')
|
115 |
components, functions = parse_gradio_code(code)
|
116 |
|
117 |
if not components and functions and isinstance(functions[0], str):
|
118 |
+
return None, f"Error: {functions[0]}"
|
119 |
|
|
|
120 |
toga_code = generate_toga_code(components, functions)
|
|
|
|
|
121 |
output_path = "/tmp/toga_app.py"
|
122 |
with open(output_path, "w") as f:
|
123 |
f.write(toga_code)
|
124 |
|
125 |
+
return output_path, "Conversion successful! Download the Toga app below."
|
126 |
except Exception as e:
|
127 |
+
return None, f"Error: {str(e)}"
|
128 |
|
129 |
# Gradio interface
|
130 |
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
131 |
gr.Markdown("# Gradio to Toga Converter")
|
132 |
+
gr.Markdown("Upload a Gradio app (.py) to convert it to a Toga desktop app.")
|
133 |
+
file_input = gr.File(label="Upload Gradio App", file_types=[".py"])
|
134 |
convert_button = gr.Button("Convert to Toga")
|
135 |
+
output = gr.File(label="Download Toga App")
|
136 |
+
status = gr.Textbox(label="Status", interactive=False)
|
137 |
|
138 |
convert_button.click(
|
139 |
fn=convert_gradio_to_toga,
|
140 |
inputs=file_input,
|
141 |
+
outputs=[output, status]
|
142 |
)
|
143 |
|
144 |
+
# Run Gradio directly for Hugging Face Spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
if __name__ == "__main__":
|
146 |
+
interface.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)), quiet=True)
|