Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -88,16 +88,12 @@ _project(project_name: str, project_path: str = DEFAULT_PROJECTPATH):
|
|
88 |
# Create the repository
|
89 |
repo = Repository(local_dir=project_path, clone_from=None)
|
90 |
repo.git_init()
|
91 |
-
|
92 |
# Add basic filesoptional, can customize this) with open(path.join(_path, "README.md"), "w") as f: f.write(f {project_name}\n\nA new Face project.")
|
93 |
-
|
94 |
# Stage all changes repo.git_add(pattern="*")
|
95 |
repo.git_commit(commit_message="Initial commit")
|
96 |
-
|
97 |
return f"""Hugging Face project '{project_name}' created successfully at '{project_path}'"""
|
98 |
except Exception as e:
|
99 |
return f"""Error creating Hugging Face project: {str(e)}"""
|
100 |
-
|
101 |
def list(project_path: str = DEFAULT_PROJECT_PATH) -> str:
|
102 |
"""Lists files in the project directory."""
|
103 |
try:
|
@@ -107,7 +103,6 @@ def list(project_path: str = DEFAULT_PROJECT_PATH) -> str:
|
|
107 |
return "\n".join(files)
|
108 |
except Exception as e:
|
109 |
return f"""Error listing project {str()}"""
|
110 |
-
|
111 |
def read_file(filepath: str, project_path: str = DEFAULT_PROPATH) -> str """Reads and returns the content of a file in the project."""
|
112 |
try:
|
113 |
_path = os.path.join(project_path, file_path)
|
@@ -125,7 +120,6 @@ def write_file(file_: str, content str project_path str =PROJECT_PATH:
|
|
125 |
return"Successfully wrote to '{_path}'"
|
126 |
except Exception as e:
|
127 |
return f"""Error writing to file: {str(e)}"""
|
128 |
-
|
129 |
def preview(project_path: str = DEFAULT_PROJECT_PATH):
|
130 |
"""Provides a preview of the project, if applicable."""
|
131 |
# Assuming a simple HTML preview for now
|
@@ -140,7 +134,6 @@ def preview(project_path: str = DEFAULT_PROJECT_PATH):
|
|
140 |
return "No 'index.html' found for preview."
|
141 |
except Exception as e:
|
142 |
return f """preview project: {str(e)}"""
|
143 |
-
|
144 |
def main():
|
145 |
.Blocks() as demo:
|
146 |
gr.Markdown("## IDEvIII: Your Hugging No- App Builder")
|
@@ -156,7 +149,6 @@ def main():
|
|
156 |
load_button = gr.Button("Load Model")
|
157 |
load_output = gr.Textbox(label="Output")
|
158 |
model_description = gr.Markdown(label="Model Description")
|
159 |
-
|
160 |
# --- Function to pop model names category ---
|
161 |
update_modeldropdown(category):
|
162 |
models = []
|
@@ -164,7 +156,6 @@ def main():
|
|
164 |
for model in api.list_models():
|
165 |
if model.pipeline_tag ==
|
166 |
models.append(model.modelId) return gr.Dropdown.update(choices=models)
|
167 |
-
|
168 |
# --- Event handler for category dropdown ---
|
169 |
model_categories.change(
|
170 |
fn=update_model_ inputs=model_categories,
|
@@ -177,13 +168,11 @@ def main():
|
|
177 |
return model_descriptions[modelname]
|
178 |
else:
|
179 |
return "Model description available."
|
180 |
-
|
181 |
model_name.change(
|
182 |
=display_model_description,
|
183 |
inputs=model_name,
|
184 |
outputs=model_description,
|
185 |
)
|
186 |
-
|
187 |
# --- Event handler to load the selected model ---
|
188 |
def load_selected_model(model_name):
|
189 |
global current_model
|
@@ -192,9 +181,7 @@ def main():
|
|
192 |
return f"""Model '{model_name}' loaded successfully!"""
|
193 |
else:
|
194 |
return f"""Error loading model '{model_name}'"""
|
195 |
-
|
196 |
load_button.click(load_selected_model, inputs=model_name, outputs=load_output)
|
197 |
-
|
198 |
# --- Chat Interface ---
|
199 |
with gr.Tab("Chat
|
200 |
chatbot gr.Chatbot(show_label=False, show_share_button=False_copy_button, likeable)
|
@@ -207,21 +194,17 @@ def main():
|
|
207 |
repetition_penalty = gr.Slider(label="Repetition penalty", value=REPETITION_PENALTY minimum=1., maximum=2.0,=0.05, interactive=True, info="Penalize repeated tokens")
|
208 |
submit_button = gr.Button(value="Send")
|
209 |
history = gr.State([])
|
210 |
-
|
211 |
run_chat(purpose: str, message: str, agent_name str, sys_prompt: str, temperature: float, max_new_tokens: int, top_p: float, repetition_penalty: float, history: List[Tuple[str, str]]) -> Tuple[List[Tuple[str,]], List[[str, str]]]:
|
212 |
if not current_model:
|
213 |
return [(history, history), "Please load a model first."]
|
214 |
-
|
215 |
def generate_response(message, history, agent_name, sys_prompt, temperature, max_new_tokens, top, repetition_penalty):
|
216 |
if not current_model:
|
217 |
return "Please load a model first."
|
218 |
-
|
219 |
conversation = [{"role": "system", "content sys_pt}]
|
220 |
for message, response history:
|
221 |
conversationappend({": "", "content": message})
|
222 |
conversation.append({"": "assistant", "content": response})
|
223 |
conversation.append({"role": "user", "content": message})
|
224 |
-
|
225 |
response = currentmodel.generate(
|
226 |
conversation,
|
227 |
max_new_tokensmax_new_tokens,
|
@@ -229,9 +212,7 @@ def main():
|
|
229 |
top_p=top_p,
|
230 |
repetition_penalty=petition_al
|
231 |
)
|
232 |
-
|
233 |
response.text.strip()
|
234 |
-
|
235 |
def create_project(project_name):
|
236 |
try:
|
237 |
repo_name = get_full_repo_name(project_name, token=HfApi().token)
|
@@ -243,20 +224,17 @@ def main():
|
|
243 |
def read_file(file_path):
|
244 |
if not os.path.exists(file_path):
|
245 |
return f"""{File_path}' does exist."""
|
246 |
-
|
247 |
try
|
248 |
with open(file, "r") as file: content = file()
|
249 |
return content
|
250 |
as e:
|
251 |
return f"""Error reading file '{file_path}': {str(e)}"""
|
252 |
-
|
253 |
def write_file(file_path, file_content): try
|
254 |
with open(file_ "w") as file:
|
255 |
file.write(_content)
|
256 |
f"""Wrote to file '{file_path}' successfully."""
|
257 |
except Exception as e:
|
258 |
return f"""Error writing to file '{file_path}': {str(e)}"""
|
259 |
-
|
260 |
def run_command(command):
|
261 |
try:
|
262 |
result =.run(command shell=True, capture_outputTrue,=True)
|
@@ -265,14 +243,11 @@ def main():
|
|
265 |
return f"Command '{command failed with exit code {.}:\n{result.stderr}"
|
266 |
except Exception:
|
267 |
return f"""Error running command '{command}': {str(e)}"""
|
268 |
-
|
269 |
def preview():
|
270 |
# Get the current working directory
|
271 |
cwd = os.getcwd()
|
272 |
-
|
273 |
# Create a temporary directory for the preview
|
274 |
temp_dir = tempfile.mkdtemp()
|
275 |
-
|
276 |
try:
|
277 |
Copy the project files the temporary directory
|
278 |
shutil.copytree(cwd, temp_dir, ignore=shutil.ignore_patterns("__py__", "*.pyc"))
|
@@ -280,14 +255,11 @@ def main():
|
|
280 |
os.chdir(temp_dir)
|
281 |
# Find the Python file (e.g., app.py, main.py)
|
282 |
main_file = next((f for f in os.listdir(".") if f.endswith(".py")), None)
|
283 |
-
|
284 |
if main_file:
|
285 |
# Run the main Python file to generate the preview
|
286 |
subprocess.run(["streamlit", "run", main_file], check)
|
287 |
-
|
288 |
# Get preview URL
|
289 |
preview_url = components.get_url(_file)
|
290 |
-
|
291 |
# Change back to original working directory
|
292 |
os.chdir(cwd)
|
293 |
# Return the preview URL return preview_url
|
@@ -297,13 +269,10 @@ def main():
|
|
297 |
return f"""Error generating preview: {str(e)}""" finally:
|
298 |
# Remove the directory
|
299 |
.rmtree(tempdir)
|
300 |
-
|
301 |
# Custom server_ = "0.0.0. # Listen on available network interfaces
|
302 |
server_port 760 # an available
|
303 |
sharegradio_link = True # Share a public URL for the app
|
304 |
-
|
305 |
# Launch the interface
|
306 |
demo.launch(server_name=server, server_portserver_port, shareshare_gradio)
|
307 |
-
|
308 |
if __name "__main__":
|
309 |
-
main()
|
|
|
88 |
# Create the repository
|
89 |
repo = Repository(local_dir=project_path, clone_from=None)
|
90 |
repo.git_init()
|
|
|
91 |
# Add basic filesoptional, can customize this) with open(path.join(_path, "README.md"), "w") as f: f.write(f {project_name}\n\nA new Face project.")
|
|
|
92 |
# Stage all changes repo.git_add(pattern="*")
|
93 |
repo.git_commit(commit_message="Initial commit")
|
|
|
94 |
return f"""Hugging Face project '{project_name}' created successfully at '{project_path}'"""
|
95 |
except Exception as e:
|
96 |
return f"""Error creating Hugging Face project: {str(e)}"""
|
|
|
97 |
def list(project_path: str = DEFAULT_PROJECT_PATH) -> str:
|
98 |
"""Lists files in the project directory."""
|
99 |
try:
|
|
|
103 |
return "\n".join(files)
|
104 |
except Exception as e:
|
105 |
return f"""Error listing project {str()}"""
|
|
|
106 |
def read_file(filepath: str, project_path: str = DEFAULT_PROPATH) -> str """Reads and returns the content of a file in the project."""
|
107 |
try:
|
108 |
_path = os.path.join(project_path, file_path)
|
|
|
120 |
return"Successfully wrote to '{_path}'"
|
121 |
except Exception as e:
|
122 |
return f"""Error writing to file: {str(e)}"""
|
|
|
123 |
def preview(project_path: str = DEFAULT_PROJECT_PATH):
|
124 |
"""Provides a preview of the project, if applicable."""
|
125 |
# Assuming a simple HTML preview for now
|
|
|
134 |
return "No 'index.html' found for preview."
|
135 |
except Exception as e:
|
136 |
return f """preview project: {str(e)}"""
|
|
|
137 |
def main():
|
138 |
.Blocks() as demo:
|
139 |
gr.Markdown("## IDEvIII: Your Hugging No- App Builder")
|
|
|
149 |
load_button = gr.Button("Load Model")
|
150 |
load_output = gr.Textbox(label="Output")
|
151 |
model_description = gr.Markdown(label="Model Description")
|
|
|
152 |
# --- Function to pop model names category ---
|
153 |
update_modeldropdown(category):
|
154 |
models = []
|
|
|
156 |
for model in api.list_models():
|
157 |
if model.pipeline_tag ==
|
158 |
models.append(model.modelId) return gr.Dropdown.update(choices=models)
|
|
|
159 |
# --- Event handler for category dropdown ---
|
160 |
model_categories.change(
|
161 |
fn=update_model_ inputs=model_categories,
|
|
|
168 |
return model_descriptions[modelname]
|
169 |
else:
|
170 |
return "Model description available."
|
|
|
171 |
model_name.change(
|
172 |
=display_model_description,
|
173 |
inputs=model_name,
|
174 |
outputs=model_description,
|
175 |
)
|
|
|
176 |
# --- Event handler to load the selected model ---
|
177 |
def load_selected_model(model_name):
|
178 |
global current_model
|
|
|
181 |
return f"""Model '{model_name}' loaded successfully!"""
|
182 |
else:
|
183 |
return f"""Error loading model '{model_name}'"""
|
|
|
184 |
load_button.click(load_selected_model, inputs=model_name, outputs=load_output)
|
|
|
185 |
# --- Chat Interface ---
|
186 |
with gr.Tab("Chat
|
187 |
chatbot gr.Chatbot(show_label=False, show_share_button=False_copy_button, likeable)
|
|
|
194 |
repetition_penalty = gr.Slider(label="Repetition penalty", value=REPETITION_PENALTY minimum=1., maximum=2.0,=0.05, interactive=True, info="Penalize repeated tokens")
|
195 |
submit_button = gr.Button(value="Send")
|
196 |
history = gr.State([])
|
|
|
197 |
run_chat(purpose: str, message: str, agent_name str, sys_prompt: str, temperature: float, max_new_tokens: int, top_p: float, repetition_penalty: float, history: List[Tuple[str, str]]) -> Tuple[List[Tuple[str,]], List[[str, str]]]:
|
198 |
if not current_model:
|
199 |
return [(history, history), "Please load a model first."]
|
|
|
200 |
def generate_response(message, history, agent_name, sys_prompt, temperature, max_new_tokens, top, repetition_penalty):
|
201 |
if not current_model:
|
202 |
return "Please load a model first."
|
|
|
203 |
conversation = [{"role": "system", "content sys_pt}]
|
204 |
for message, response history:
|
205 |
conversationappend({": "", "content": message})
|
206 |
conversation.append({"": "assistant", "content": response})
|
207 |
conversation.append({"role": "user", "content": message})
|
|
|
208 |
response = currentmodel.generate(
|
209 |
conversation,
|
210 |
max_new_tokensmax_new_tokens,
|
|
|
212 |
top_p=top_p,
|
213 |
repetition_penalty=petition_al
|
214 |
)
|
|
|
215 |
response.text.strip()
|
|
|
216 |
def create_project(project_name):
|
217 |
try:
|
218 |
repo_name = get_full_repo_name(project_name, token=HfApi().token)
|
|
|
224 |
def read_file(file_path):
|
225 |
if not os.path.exists(file_path):
|
226 |
return f"""{File_path}' does exist."""
|
|
|
227 |
try
|
228 |
with open(file, "r") as file: content = file()
|
229 |
return content
|
230 |
as e:
|
231 |
return f"""Error reading file '{file_path}': {str(e)}"""
|
|
|
232 |
def write_file(file_path, file_content): try
|
233 |
with open(file_ "w") as file:
|
234 |
file.write(_content)
|
235 |
f"""Wrote to file '{file_path}' successfully."""
|
236 |
except Exception as e:
|
237 |
return f"""Error writing to file '{file_path}': {str(e)}"""
|
|
|
238 |
def run_command(command):
|
239 |
try:
|
240 |
result =.run(command shell=True, capture_outputTrue,=True)
|
|
|
243 |
return f"Command '{command failed with exit code {.}:\n{result.stderr}"
|
244 |
except Exception:
|
245 |
return f"""Error running command '{command}': {str(e)}"""
|
|
|
246 |
def preview():
|
247 |
# Get the current working directory
|
248 |
cwd = os.getcwd()
|
|
|
249 |
# Create a temporary directory for the preview
|
250 |
temp_dir = tempfile.mkdtemp()
|
|
|
251 |
try:
|
252 |
Copy the project files the temporary directory
|
253 |
shutil.copytree(cwd, temp_dir, ignore=shutil.ignore_patterns("__py__", "*.pyc"))
|
|
|
255 |
os.chdir(temp_dir)
|
256 |
# Find the Python file (e.g., app.py, main.py)
|
257 |
main_file = next((f for f in os.listdir(".") if f.endswith(".py")), None)
|
|
|
258 |
if main_file:
|
259 |
# Run the main Python file to generate the preview
|
260 |
subprocess.run(["streamlit", "run", main_file], check)
|
|
|
261 |
# Get preview URL
|
262 |
preview_url = components.get_url(_file)
|
|
|
263 |
# Change back to original working directory
|
264 |
os.chdir(cwd)
|
265 |
# Return the preview URL return preview_url
|
|
|
269 |
return f"""Error generating preview: {str(e)}""" finally:
|
270 |
# Remove the directory
|
271 |
.rmtree(tempdir)
|
|
|
272 |
# Custom server_ = "0.0.0. # Listen on available network interfaces
|
273 |
server_port 760 # an available
|
274 |
sharegradio_link = True # Share a public URL for the app
|
|
|
275 |
# Launch the interface
|
276 |
demo.launch(server_name=server, server_portserver_port, shareshare_gradio)
|
|
|
277 |
if __name "__main__":
|
278 |
+
main()
|