Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, send_file, render_template_string
|
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", "placeholder='Enter Text'"),
|
12 |
+
"Image": ("toga.Label", "Placeholder: Use file picker for image"),
|
13 |
+
"Button": ("toga.Button", "on_press=button_handler"),
|
14 |
+
"Markdown": ("toga.Label", ""),
|
15 |
+
"Audio": ("toga.Label", "Placeholder: Audio not supported"),
|
16 |
+
"Video": ("toga.Label", "Placeholder: Video not supported"),
|
17 |
+
}
|
18 |
+
|
19 |
+
# Parse Gradio app code and extract components
|
20 |
+
def parse_gradio_code(code):
|
21 |
+
try:
|
22 |
+
tree = ast.parse(code)
|
23 |
+
components = []
|
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
|
34 |
+
args = [ast.dump(arg, annotate_fields=False) for arg in node.args]
|
35 |
+
kwargs = {kw.arg: ast.dump(kw.value, annotate_fields=False) for kw in node.keywords}
|
36 |
+
components.append({"type": component, "args": args, "kwargs": kwargs})
|
37 |
+
|
38 |
+
return components, functions
|
39 |
+
except Exception as e:
|
40 |
+
return [], [str(e)]
|
41 |
+
|
42 |
+
# Generate Toga code from parsed components
|
43 |
+
def generate_toga_code(components, functions):
|
44 |
+
toga_code = [
|
45 |
+
"import toga",
|
46 |
+
"from toga.style import Pack",
|
47 |
+
"from toga.style.pack import COLUMN, ROW",
|
48 |
+
"import os",
|
49 |
+
"",
|
50 |
+
"# Placeholder for original functions",
|
51 |
+
"# Copy your original function definitions here",
|
52 |
+
f"# Detected functions: {', '.join(functions) if functions else 'None'}",
|
53 |
+
"",
|
54 |
+
"def build(app):",
|
55 |
+
" box = toga.Box(style=Pack(direction=COLUMN, padding=10))",
|
56 |
+
""
|
57 |
+
]
|
58 |
+
|
59 |
+
for idx, comp in enumerate(components):
|
60 |
+
toga_comp, extra = GRADIO_TO_TOGA.get(comp["type"], ("toga.Label", f"Placeholder: {comp['type']} not supported"))
|
61 |
+
|
62 |
+
# Handle component-specific logic
|
63 |
+
if comp["type"] == "Textbox":
|
64 |
+
label = comp["kwargs"].get("label", f"'Text Input {idx}'")
|
65 |
+
toga_code.append(f" label_{idx} = toga.Label({label}, style=Pack(padding=(5, 5, 0, 5)))")
|
66 |
+
toga_code.append(f" input_{idx} = {toga_comp}(style=Pack(padding=5))")
|
67 |
+
toga_code.append(f" box.add(label_{idx})")
|
68 |
+
toga_code.append(f" box.add(input_{idx})")
|
69 |
+
|
70 |
+
elif comp["type"] == "Image":
|
71 |
+
toga_code.append(f" image_label_{idx} = toga.Label('Image: None selected', style=Pack(padding=5))")
|
72 |
+
toga_code.append(f" def select_image_{idx}(widget):")
|
73 |
+
toga_code.append(f" path = app.main_window.open_file_dialog('Select Image', file_types=['png', 'jpg', 'jpeg'])")
|
74 |
+
toga_code.append(f" if path:")
|
75 |
+
toga_code.append(f" image_label_{idx}.text = f'Image: {{path}}'")
|
76 |
+
toga_code.append(f" image_button_{idx} = toga.Button('Upload Image', on_press=select_image_{idx}, style=Pack(padding=5))")
|
77 |
+
toga_code.append(f" box.add(toga.Label('Upload Image', style=Pack(padding=(5, 5, 0, 5))))")
|
78 |
+
toga_code.append(f" box.add(image_button_{idx})")
|
79 |
+
toga_code.append(f" box.add(image_label_{idx})")
|
80 |
+
|
81 |
+
elif comp["type"] == "Button":
|
82 |
+
label = comp["kwargs"].get("value", f"'Submit {idx}'")
|
83 |
+
toga_code.append(f" def button_handler_{idx}(widget):")
|
84 |
+
toga_code.append(f" # Placeholder: Implement logic for {functions[0] if functions else 'function'}")
|
85 |
+
toga_code.append(f" pass")
|
86 |
+
toga_code.append(f" button_{idx} = {toga_comp}(label={label}, style=Pack(padding=5))")
|
87 |
+
toga_code.append(f" box.add(button_{idx})")
|
88 |
+
|
89 |
+
elif comp["type"] == "Markdown":
|
90 |
+
text = comp["args"][0] if comp["args"] else "'Markdown Text'"
|
91 |
+
toga_code.append(f" markdown_{idx} = {toga_comp}({text}, style=Pack(padding=5, font_size=16))")
|
92 |
+
toga_code.append(f" box.add(markdown_{idx})")
|
93 |
+
|
94 |
+
else:
|
95 |
+
toga_code.append(f" placeholder_{idx} = {toga_comp}('{extra}', style=Pack(padding=5))")
|
96 |
+
toga_code.append(f" box.add(placeholder_{idx})")
|
97 |
+
|
98 |
+
toga_code.extend([
|
99 |
+
"",
|
100 |
+
" return box",
|
101 |
+
"",
|
102 |
+
"def main():",
|
103 |
+
" return toga.App('Gradio to Toga App', 'org.example.gradio2toga', startup=build)",
|
104 |
+
"",
|
105 |
+
"if __name__ == '__main__':",
|
106 |
+
" main().main_loop()"
|
107 |
+
])
|
108 |
+
|
109 |
+
return "\n".join(toga_code)
|
110 |
+
|
111 |
+
# Gradio interface for file upload
|
112 |
+
def convert_gradio_to_toga(file):
|
113 |
+
if not file:
|
114 |
+
return "Please upload a Gradio app Python file."
|
115 |
+
|
116 |
+
try:
|
117 |
+
# Read uploaded file
|
118 |
+
code = file.decode('utf-8')
|
119 |
+
components, functions = parse_gradio_code(code)
|
120 |
+
|
121 |
+
if not components and functions and isinstance(functions[0], str):
|
122 |
+
return f"Error parsing code: {functions[0]}"
|
123 |
+
|
124 |
+
# Generate Toga code
|
125 |
+
toga_code = generate_toga_code(components, functions)
|
126 |
+
|
127 |
+
# Save Toga code to a temporary file
|
128 |
+
output_path = "toga_app.py"
|
129 |
+
with open(output_path, "w") as f:
|
130 |
+
f.write(toga_code)
|
131 |
+
|
132 |
+
return send_file(output_path, as_attachment=True, download_name="toga_app.py")
|
133 |
+
except Exception as e:
|
134 |
+
return f"Error: {str(e)}"
|
135 |
+
|
136 |
+
# Gradio interface
|
137 |
+
def create_gradio_interface():
|
138 |
+
with gr.Blocks() as interface:
|
139 |
+
gr.Markdown("# Gradio to Toga Converter")
|
140 |
+
file_input = gr.File(label="Upload Gradio Python File")
|
141 |
+
convert_button = gr.Button("Convert to Toga")
|
142 |
+
output = gr.File(label="Download Toga App")
|
143 |
+
|
144 |
+
convert_button.click(
|
145 |
+
fn=convert_gradio_to_toga,
|
146 |
+
inputs=file_input,
|
147 |
+
outputs=output
|
148 |
+
)
|
149 |
+
return interface
|
150 |
+
|
151 |
+
# Flask route
|
152 |
+
@app.route('/')
|
153 |
+
def serve_gradio():
|
154 |
+
interface = create_gradio_interface()
|
155 |
+
# For Hugging Face Spaces, launch Gradio
|
156 |
+
interface.launch(share=False, server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
157 |
+
return render_template_string("""
|
158 |
+
<!DOCTYPE html>
|
159 |
+
<html>
|
160 |
+
<head>
|
161 |
+
<title>Gradio to Toga Converter</title>
|
162 |
+
</head>
|
163 |
+
<body>
|
164 |
+
<h1>Gradio to Toga Converter</h1>
|
165 |
+
<p>Access the Gradio interface to upload your Gradio app and download the Toga equivalent.</p>
|
166 |
+
</body>
|
167 |
+
</html>
|
168 |
+
""")
|
169 |
+
|
170 |
+
if __name__ == "__main__":
|
171 |
+
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|