import sys import subprocess from typing import List, Dict, Optional, Union, Tuple from functools import lru_cache from dataclasses import dataclass, field from enum import Enum, auto import webbrowser import tempfile import os import importlib class AppType(Enum): WEB_APP = auto() GRADIO_APP = auto() STREAMLIT_APP = auto() REACT_APP = auto() @dataclass(frozen=True) class Code: content: str language: str @dataclass(frozen=True) class Prompt: content: str @dataclass(frozen=True) class Space: content: str @dataclass(frozen=True) class Tutorial: content: str @dataclass(frozen=True) class File: name: str content: str language: str @dataclass(frozen=True) class AppInfo: name: str description: str features: Tuple[str, ...] dependencies: Tuple[str, ...] space: Optional[Space] = None tutorial: Optional[Tutorial] = None @dataclass(frozen=True) class App: code: Code def run(self): raise NotImplementedError("Subclasses must implement run method") @dataclass(frozen=True) class WebApp(App): 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}") @dataclass(frozen=True) class GradioApp(App): def run(self): with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as f: f.write(self.code.content) try: subprocess.run([sys.executable, f.name], check=True) except subprocess.CalledProcessError: print("Error running Gradio app. Make sure Gradio is installed: pip install gradio") finally: os.unlink(f.name) @dataclass(frozen=True) class StreamlitApp(App): def run(self): with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as f: f.write(self.code.content) try: subprocess.run([sys.executable, "-m", "streamlit", "run", f.name], check=True) except subprocess.CalledProcessError: print("Error running Streamlit app. Make sure Streamlit is installed: pip install streamlit") finally: os.unlink(f.name) @dataclass(frozen=True) class ReactApp(App): 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: class AppFactory: @staticmethod @lru_cache(maxsize=128) 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. """ ) @staticmethod @lru_cache(maxsize=128) 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)} """ ) @staticmethod @lru_cache(maxsize=128) 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". """ ) @staticmethod 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) @staticmethod @lru_cache(maxsize=128) def _create_web_app(app_info: AppInfo) -> WebApp: code = Code( content=f"""
{app_info.description}
""", language="html" ) return WebApp(code=code) @staticmethod @lru_cache(maxsize=128) def _create_gradio_app(app_info: AppInfo) -> GradioApp: code = Code( content=""" 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) @staticmethod @lru_cache(maxsize=128) 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) @staticmethod @lru_cache(maxsize=128) def _create_react_app(app_info: AppInfo) -> ReactApp: code = Code( content=f""" import React from 'react'; function App() {{ return ({app_info.description}