Spaces:
Sleeping
Sleeping
acecalisto3
commited on
Update cust_types.py
Browse files- cust_types.py +111 -28
cust_types.py
CHANGED
@@ -8,54 +8,137 @@ class AppType(Enum):
|
|
8 |
STREAMLIT_APP = "streamlit_app"
|
9 |
REACT_APP = "react_app"
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
class Prompt:
|
12 |
def __init__(self, prompt: str):
|
13 |
self.prompt = prompt
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
class Space:
|
16 |
def __init__(self, space: str):
|
17 |
self.space = space
|
18 |
|
|
|
|
|
|
|
19 |
class Tutorial:
|
20 |
def __init__(self, tutorial: str):
|
21 |
self.tutorial = tutorial
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
self.
|
28 |
|
29 |
class App:
|
30 |
-
def __init__(self,
|
|
|
31 |
self.app_type = app_type
|
32 |
-
self.
|
33 |
-
self.
|
34 |
-
self.
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
class WebApp(App):
|
40 |
-
def __init__(self, code:
|
41 |
-
super().__init__(
|
42 |
-
|
43 |
-
|
|
|
|
|
44 |
|
45 |
class GradioApp(App):
|
46 |
-
def __init__(self, code:
|
47 |
-
super().__init__(
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
|
51 |
class StreamlitApp(App):
|
52 |
-
def __init__(self, code:
|
53 |
-
super().__init__(
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
|
57 |
class ReactApp(App):
|
58 |
-
def __init__(self, code:
|
59 |
-
super().__init__(
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
STREAMLIT_APP = "streamlit_app"
|
9 |
REACT_APP = "react_app"
|
10 |
|
11 |
+
import hashlib
|
12 |
+
import os
|
13 |
+
import time
|
14 |
+
from typing import Optional
|
15 |
+
|
16 |
+
class Code:
|
17 |
+
def __init__(self, code: str, language: str):
|
18 |
+
self.code = code
|
19 |
+
self.language = language
|
20 |
+
|
21 |
+
def get_code_length(self) -> int:
|
22 |
+
return len(self.code)
|
23 |
+
|
24 |
+
def get_language(self) -> str:
|
25 |
+
return self.language
|
26 |
+
|
27 |
+
def hash_code(self) -> str:
|
28 |
+
return hashlib.sha256(self.code.encode()).hexdigest()
|
29 |
+
|
30 |
class Prompt:
|
31 |
def __init__(self, prompt: str):
|
32 |
self.prompt = prompt
|
33 |
|
34 |
+
def get_prompt_length(self) -> int:
|
35 |
+
return len(self.prompt)
|
36 |
+
|
37 |
+
def preview_prompt(self, length: int = 50) -> str:
|
38 |
+
return self.prompt[:length] + ('...' if len(self.prompt) > length else '')
|
39 |
+
|
40 |
+
class AppType:
|
41 |
+
def __init__(self, app_type: str):
|
42 |
+
self.app_type = app_type
|
43 |
+
|
44 |
+
def is_web_app(self) -> bool:
|
45 |
+
return self.app_type.lower() == 'web'
|
46 |
+
|
47 |
+
def is_gradio_app(self) -> bool:
|
48 |
+
return self.app_type.lower() == 'gradio'
|
49 |
+
|
50 |
+
def is_streamlit_app(self) -> bool:
|
51 |
+
return self.app_type.lower() == 'streamlit'
|
52 |
+
|
53 |
+
def is_react_app(self) -> bool:
|
54 |
+
return self.app_type.lower() == 'react'
|
55 |
+
|
56 |
+
class File:
|
57 |
+
def __init__(self, path: str, content: str):
|
58 |
+
self.path = path
|
59 |
+
self.content = content
|
60 |
+
|
61 |
+
def save(self) -> None:
|
62 |
+
with open(self.path, 'w') as file:
|
63 |
+
file.write(self.content)
|
64 |
+
|
65 |
+
def load(self) -> None:
|
66 |
+
if os.path.exists(self.path):
|
67 |
+
with open(self.path, 'r') as file:
|
68 |
+
self.content = file.read()
|
69 |
+
|
70 |
+
def get_file_size(self) -> int:
|
71 |
+
return len(self.content)
|
72 |
+
|
73 |
class Space:
|
74 |
def __init__(self, space: str):
|
75 |
self.space = space
|
76 |
|
77 |
+
def get_space_name(self) -> str:
|
78 |
+
return self.space
|
79 |
+
|
80 |
class Tutorial:
|
81 |
def __init__(self, tutorial: str):
|
82 |
self.tutorial = tutorial
|
83 |
|
84 |
+
def get_tutorial_length(self) -> int:
|
85 |
+
return len(self.tutorial)
|
86 |
+
|
87 |
+
def preview_tutorial(self, length: int = 100) -> str:
|
88 |
+
return self.tutorial[:length] + ('...' if len(self.tutorial) > length else '')
|
89 |
|
90 |
class App:
|
91 |
+
def __init__(self, code: Code, app_type: AppType, space: Space, tutorial: Tutorial):
|
92 |
+
self.code = code
|
93 |
self.app_type = app_type
|
94 |
+
self.space = space
|
95 |
+
self.tutorial = tutorial
|
96 |
+
self._id = self.generate_id()
|
97 |
+
|
98 |
+
def generate_id(self) -> str:
|
99 |
+
return f"app-{hashlib.md5(str(time.time()).encode()).hexdigest()}"
|
100 |
+
|
101 |
+
def launch(self) -> None:
|
102 |
+
print(f"Launching app {self._id}...")
|
103 |
|
104 |
class WebApp(App):
|
105 |
+
def __init__(self, code: Code, app_type: AppType, space: Space, tutorial: Tutorial):
|
106 |
+
super().__init__(code, app_type, space, tutorial)
|
107 |
+
|
108 |
+
def launch(self) -> None:
|
109 |
+
super().launch()
|
110 |
+
print("WebApp specific launch sequence...")
|
111 |
|
112 |
class GradioApp(App):
|
113 |
+
def __init__(self, code: Code, app_type: AppType, space: Space, tutorial: Tutorial):
|
114 |
+
super().__init__(code, app_type, space, tutorial)
|
115 |
+
|
116 |
+
def launch(self) -> None:
|
117 |
+
super().launch()
|
118 |
+
print("GradioApp specific launch sequence...")
|
119 |
|
120 |
class StreamlitApp(App):
|
121 |
+
def __init__(self, code: Code, app_type: AppType, space: Space, tutorial: Tutorial):
|
122 |
+
super().__init__(code, app_type, space, tutorial)
|
123 |
+
|
124 |
+
def launch(self) -> None:
|
125 |
+
super().launch()
|
126 |
+
print("StreamlitApp specific launch sequence...")
|
127 |
|
128 |
class ReactApp(App):
|
129 |
+
def __init__(self, code: Code, app_type: AppType, space: Space, tutorial: Tutorial):
|
130 |
+
super().__init__(code, app_type, space, tutorial)
|
131 |
+
|
132 |
+
def launch(self) -> None:
|
133 |
+
super().launch()
|
134 |
+
print("ReactApp specific launch sequence...")
|
135 |
+
|
136 |
+
# Example usage
|
137 |
+
if __name__ == "__main__":
|
138 |
+
code = Code("example code", "python")
|
139 |
+
app_type = AppType("web")
|
140 |
+
space = Space("example space")
|
141 |
+
tutorial = Tutorial("example tutorial")
|
142 |
+
|
143 |
+
app = WebApp(code, app_type, space, tutorial)
|
144 |
+
app.launch()
|