acecalisto3 commited on
Commit
e64f720
1 Parent(s): 1e94b61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -10
app.py CHANGED
@@ -10,6 +10,9 @@ from huggingface_hub import HfApi, create_repo
10
  import importlib
11
  import os
12
  from huggingface_hub import HfApi, create_repo
 
 
 
13
 
14
  hf_api = HfApi()
15
 
@@ -288,17 +291,59 @@ def run_docker_container(image_name, port):
288
  container = client.containers.run(image_name, detach=True, ports={f'{port}/tcp': port})
289
  return container
290
 
291
- def deploy_space_to_hf(project_name, hf_token):
292
- repository_name = f"my-awesome-space_{datetime.now().timestamp()}"
293
- files = get_built_space_files()
294
- commit_response = deploy_to_git(project_name, repository_name, files)
295
- if commit_response:
296
- api = HfApi(token=hf_token) # Corrected import statement
297
- api.create(repository_name, files=files, push_to_hub=True)
298
- return f"Space '{repository_name}' deployed successfully."
299
- else:
300
- return "Failed to commit changes to Space."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  if __name__ == "__main__":
303
  db.create_all() # Create the database tables if they don't exist
304
  main()
 
10
  import importlib
11
  import os
12
  from huggingface_hub import HfApi, create_repo
13
+ from transformers import pipeline
14
+ codex_pipeline = pipeline("code-generation", model="EleutherAI/code-davinci-002")
15
+ import huggingface_cli
16
 
17
  hf_api = HfApi()
18
 
 
291
  container = client.containers.run(image_name, detach=True, ports={f'{port}/tcp': port})
292
  return container
293
 
294
+ def generate_app(user_idea, project_name):
295
+ # Extract key information from the user idea
296
+ summary = nlp_pipeline(user_idea, max_length=50, min_length=10)[0]["summary_text"]
297
+
298
+ # Create project directory if it doesn't exist
299
+ project_path = create_project(project_name)
300
+
301
+ # Generate code using Codex
302
+ prompt = f"Create a simple Streamlit app for the project named '{project_name}'. The app should display the following summary: '{summary}'."
303
+ generated_code = codex_pipeline(prompt)[0]['generated_text']
304
+
305
+ # Save the generated code to a file in the project directory
306
+ with open(os.path.join(project_path, "app.py"), "w") as f:
307
+ f.write(generated_code)
308
+
309
+ # Deploy the app to Hugging Face Spaces
310
+ deploy_app_to_hf_spaces(project_name, token, generated_code)
311
+
312
+ return generated_code, project_path
313
+
314
+ def deploy_app_to_hf_spaces(project_name, token, generated_code):
315
+ repo_name = f"hf-{project_name}"
316
+ repo_id = hf_api.changelog.get_repo_id(repo_name)
317
+
318
+ if not repo_id:
319
+ create_repo = huggingface_cli.create_repo(repo_name, "public", "Streamlit App", token)
320
+ repo_id = create_repo["repo_id"]
321
 
322
+ # Save the generated code to a temporary file
323
+
324
+ temp_file = "temp_code.py"
325
+ with open(temp_file, "w") as f:
326
+ f.write(generated_code)
327
+
328
+ # Upload the file to Hugging Face Spaces
329
+ api.upload_files(repo_id, [temp_file], token)
330
+
331
+ # Delete the temporary file
332
+ os.remove(temp_file)
333
+
334
+ def launch_chatapp(project_path):
335
+ # ... (previous code)
336
+
337
+ if st.button("Build & Deploy"):
338
+ token = st.text_input("Enter your Hugging Face token:", type="password")
339
+ generated_code, project_path = generate_app(st.session_state["user_idea"], project_name)
340
+ st.write(f"Generated code: {generated_code}")
341
+ if token:
342
+ deploy_app_to_hf_spaces(project_name, token, generated_code)
343
+ st.success("App deployed to Hugging Face Spaces!")
344
+ else:
345
+ st.error("Please enter a Hugging Face token to deploy the app.")
346
+
347
  if __name__ == "__main__":
348
  db.create_all() # Create the database tables if they don't exist
349
  main()