Spaces:
Sleeping
Sleeping
import sys | |
import subprocess | |
from typing import List, Dict, Optional, Union | |
from functools import lru_cache | |
from dataclasses import dataclass, field | |
from enum import Enum, auto | |
import webbrowser | |
import tempfile | |
import os | |
class WebApp: | |
code: Code | |
def run(self): | |
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.html') as f: | |
f.write(self.code.content) | |
webbrowser.open('file://' + f.name) | |
print(f"Opened WebApp in default browser. Temporary file: {f.name}") | |
class GradioApp: | |
code: Code | |
def run(self): | |
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as f: | |
f.write(self.code.content) | |
subprocess.run([sys.executable, f.name]) | |
os.unlink(f.name) | |
class StreamlitApp: | |
code: Code | |
def run(self): | |
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as f: | |
f.write(self.code.content) | |
subprocess.run([sys.executable, "-m", "streamlit", "run", f.name]) | |
os.unlink(f.name) | |
class ReactApp: | |
code: Code | |
def run(self): | |
print("To run a React app, you need to set up a proper React environment.") | |
print("Here's how you might typically run a React app:") | |
print("1. Make sure you have Node.js and npm installed") | |
print("2. Create a new React app: npx create-react-app my-app") | |
print("3. Replace the contents of src/App.js with the generated code") | |
print("4. Run the app: npm start") | |
print("\nHere's the code for your App.js:") | |
print(self.code.content) | |
class AppFactory: | |
def create_prompt(app_type: AppType, app_info: AppInfo) -> Prompt: | |
return Prompt( | |
content=f""" | |
Create a {app_type.name} web application with the following details: | |
Name: {app_info.name} | |
Description: {app_info.description} | |
Features: {', '.join(app_info.features)} | |
Dependencies: {', '.join(app_info.dependencies)} | |
Space: {app_info.space.content if app_info.space else 'N/A'} | |
Tutorial: {app_info.tutorial.content if app_info.tutorial else 'N/A'} | |
Please generate the code for this application. | |
""" | |
) | |
def create_space(app_info: AppInfo) -> Space: | |
return Space( | |
content=f""" | |
{app_info.name} | |
{app_info.description} | |
Features: {', '.join(app_info.features)} | |
Dependencies: {', '.join(app_info.dependencies)} | |
""" | |
) | |
def create_app_type_prompt(app_type: AppType, app_info: AppInfo) -> Prompt: | |
return Prompt( | |
content=f""" | |
Is the following web application a {app_type.name}? | |
{app_info.name} | |
{app_info.description} | |
Features: {', '.join(app_info.features)} | |
Dependencies: {', '.join(app_info.dependencies)} | |
Please answer with either "Yes" or "No". | |
""" | |
) | |
def get_app(app_type: AppType, app_info: AppInfo) -> App: | |
app_creators = { | |
AppType.WEB_APP: AppFactory._create_web_app, | |
AppType.GRADIO_APP: AppFactory._create_gradio_app, | |
AppType.STREAMLIT_APP: AppFactory._create_streamlit_app, | |
AppType.REACT_APP: AppFactory._create_react_app, | |
} | |
return app_creators[app_type](app_info) | |
def _create_web_app(app_info: AppInfo) -> WebApp: | |
code = Code( | |
content=f""" | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>{app_info.name}</title> | |
</head> | |
<body> | |
<h1>{app_info.name}</h1> | |
<p>{app_info.description}</p> | |
</body> | |
</html> | |
""", | |
language="html" | |
) | |
return WebApp(code=code) | |
def _create_gradio_app(app_info: AppInfo) -> GradioApp: | |
code = Code( | |
content=f""" | |
import gradio as gr | |
def greet(name): | |
return f"Hello, {{name}}!" | |
demo = gr.Interface(greet, "text", "text") | |
if __name__ == "__main__": | |
demo.launch() | |
""", | |
language="python" | |
) | |
return GradioApp(code=code) | |
def _create_streamlit_app(app_info: AppInfo) -> StreamlitApp: | |
code = Code( | |
content=f""" | |
import streamlit as st | |
st.title('{app_info.name}') | |
st.write('{app_info.description}') | |
""", | |
language="python" | |
) | |
return StreamlitApp(code=code) | |
def _create_react_app(app_info: AppInfo) -> ReactApp: | |
code = Code( | |
content=f""" | |
import React from 'react'; | |
function App() {{ | |
return ( | |
<div className="App"> | |
<h1>{app_info.name}</h1> | |
<p>{app_info.description}</p> | |
</div> | |
); | |
}} | |
export default App; | |
""", | |
language="javascript" | |
) | |
return ReactApp(code=code) | |
def parse_tutorial(app_info: AppInfo) -> Tutorial: | |
return Tutorial( | |
content=f""" | |
## {app_info.name} Tutorial | |
**Introduction** | |
{app_info.description} | |
**Prerequisites** | |
* Basic knowledge of web development | |
* Familiarity with {', '.join(app_info.dependencies)} | |
**Steps** | |
{chr(10).join(f"{i+1}. {feature}" for i, feature in enumerate(app_info.features))} | |
**Conclusion** | |
Congratulations! You have successfully created a {app_info.name} application. | |
""" | |
) | |
def generate_files(app_type: AppType, app_info: AppInfo) -> List[File]: | |
app = AppFactory.get_app(app_type, app_info) | |
file_name = { | |
AppType.WEB_APP: "index.html", | |
AppType.GRADIO_APP: "app.py", | |
AppType.STREAMLIT_APP: "app.py", | |
AppType.REACT_APP: "App.js", | |
}[app_type] | |
return [File(name=file_name, content=app.code.content, language=app.code.language)] | |
def run_app(app: App): | |
app.run() | |
if __name__ == "__main__": | |
app_info = AppInfo( | |
name="My Cool App", | |
description="A simple web application", | |
features=["Feature 1", "Feature 2", "Feature 3"], | |
dependencies=["Python", "JavaScript"], | |
) | |
# Create and run a WebApp | |
web_app = AppFactory.get_app(AppType.WEB_APP, app_info) | |
AppFactory.run_app(web_app) | |
# Create and run a GradioApp | |
gradio_app = AppFactory.get_app(AppType.GRADIO_APP, app_info) | |
AppFactory.run_app(gradio_app) | |
# Create and run a StreamlitApp | |
streamlit_app = AppFactory.get_app(AppType.STREAMLIT_APP, app_info) | |
AppFactory.run_app(streamlit_app) | |
# Create and display info for a ReactApp | |
react_app = AppFactory.get_app(AppType.REACT_APP, app_info) | |
AppFactory.run_app(react_app) |