Spaces:
Sleeping
Sleeping
from typing import List, Dict, Optional | |
from cust_types import ( | |
Code, | |
Prompt, | |
AppType, | |
File, | |
Space, | |
Tutorial, | |
App, | |
WebApp, | |
GradioApp, | |
StreamlitApp, | |
ReactApp, | |
Code, | |
) | |
def createLlamaPrompt( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> Prompt: | |
""" | |
Generates a prompt for a Llama model to create a web application. | |
""" | |
prompt = f""" | |
I need you to help me create a {app_type} web application. | |
The application name is: {app_name} | |
The application description is: {app_description} | |
The application features are: {app_features} | |
The application dependencies are: {app_dependencies} | |
The application space is: {app_space} | |
The application tutorial is: {app_tutorial} | |
Please generate the code for the application. | |
""" | |
return Prompt(prompt=prompt) | |
def createSpace( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> Space: | |
""" | |
Generates a space for a web application. | |
""" | |
space = f""" | |
{app_name} | |
{app_description} | |
{app_features} | |
{app_dependencies} | |
{app_space} | |
{app_tutorial} | |
""" | |
return Space(space=space) | |
def isPythonOrGradioAppPrompt( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> Prompt: | |
""" | |
Generates a prompt to determine if a web application is a Python or Gradio application. | |
""" | |
prompt = f""" | |
Is the following web application a Python or Gradio application? | |
{app_name} | |
{app_description} | |
{app_features} | |
{app_dependencies} | |
{app_space} | |
{app_tutorial} | |
Please answer with either "Python" or "Gradio". | |
""" | |
return Prompt(prompt=prompt) | |
def isReactAppPrompt( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> Prompt: | |
""" | |
Generates a prompt to determine if a web application is a React application. | |
""" | |
prompt = f""" | |
Is the following web application a React application? | |
{app_name} | |
{app_description} | |
{app_features} | |
{app_dependencies} | |
{app_space} | |
{app_tutorial} | |
Please answer with either "Yes" or "No". | |
""" | |
return Prompt(prompt=prompt) | |
def isStreamlitAppPrompt( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> Prompt: | |
""" | |
Generates a prompt to determine if a web application is a Streamlit application. | |
""" | |
prompt = f""" | |
Is the following web application a Streamlit application? | |
{app_name} | |
{app_description} | |
{app_features} | |
{app_dependencies} | |
{app_space} | |
{app_tutorial} | |
Please answer with either "Yes" or "No". | |
""" | |
return Prompt(prompt=prompt) | |
def getWebApp( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> WebApp: | |
""" | |
Generates code for a web application. | |
""" | |
code = f""" | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>{app_name}</title> | |
</head> | |
<body> | |
<h1>{app_name}</h1> | |
<p>{app_description}</p> | |
</body> | |
</html> | |
""" | |
return WebApp(code=code, language="html") | |
def getGradioApp( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> GradioApp: | |
""" | |
Generates code for a Gradio application. | |
""" | |
code = f""" | |
import gradio as gr | |
def greet(name): | |
return f"Hello, {name}!" | |
demo = gr.Interface(greet, "text", "text") | |
if __name__ == "__main__": | |
demo.launch() | |
""" | |
return GradioApp(code=code, language="python") | |
def getReactApp( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> ReactApp: | |
""" | |
Generates code for a React application. | |
""" | |
code = f""" | |
import React from 'react'; | |
function App() {{ | |
return ( | |
<div className="App"> | |
<h1>{app_name}</h1> | |
<p>{app_description}</p> | |
</div> | |
); | |
}} | |
export default App; | |
""" | |
return ReactApp(code=code, language="javascript") | |
def getStreamlitApp( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> StreamlitApp: | |
""" | |
Generates code for a Streamlit application. | |
""" | |
code = f""" | |
import streamlit as st | |
st.title('{app_name}') | |
st.write('{app_description}') | |
""" | |
return StreamlitApp(code=code, language="python") | |
def parseTutorial( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> Tutorial: | |
""" | |
Parses a tutorial for a web application. | |
""" | |
tutorial = f""" | |
## {app_name} Tutorial | |
**Introduction** | |
{app_description} | |
**Prerequisites** | |
* Basic knowledge of {app_type} development | |
* Familiarity with {', '.join(app_dependencies)} | |
**Steps** | |
1. {app_features[0]} | |
2. {app_features[1]} | |
3. {app_features[2]} | |
**Conclusion** | |
Congratulations! You have successfully created a {app_name} application. | |
""" | |
return Tutorial(tutorial=tutorial) | |
def generateFiles( | |
app_type: AppType, | |
app_name: str, | |
app_description: str, | |
app_features: List[str], | |
app_dependencies: List[str], | |
app_space: Optional[Space] = None, | |
app_tutorial: Optional[Tutorial] = None, | |
) -> List[File]: | |
""" | |
Generates files for a web application. | |
""" | |
files = [] | |
if app_type == AppType.WEB_APP: | |
files.append(File(name="index.html", content=getWebApp(app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial).code, language="html")) | |
elif app_type == AppType.GRADIO_APP: | |
files.append(File(name="app.py", content=getGradioApp(app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial).code, language="python")) | |
elif app_type == AppType.STREAMLIT_APP: | |
files.append(File(name="app.py", content=getStreamlitApp(app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial).code, language="python")) | |
elif app_type == AppType.REACT_APP: | |
files.append(File(name="App.js", content=getReactApp(app_type, app_name, app_description, app_features, app_dependencies, app_space, app_tutorial).code, language="javascript")) | |
return files |