Spaces:
Sleeping
Sleeping
File size: 7,747 Bytes
42f2529 ffbc586 42f2529 6a7ff95 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
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 |