engrharis commited on
Commit
fb36264
·
verified ·
1 Parent(s): 07e4fe6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from huggingface_hub import HfApi, create_repo, upload_file
3
+ import os
4
+
5
+ # App title
6
+ st.title("HF Helper - Manage Your Hugging Face Spaces Easily")
7
+
8
+ # Use session state to store the token
9
+ if "hf_token" not in st.session_state:
10
+ st.session_state["hf_token"] = None
11
+
12
+ # Ask for token input if not already saved
13
+ if st.session_state["hf_token"] is None:
14
+ token_input = st.text_input("Enter your Hugging Face Access Token:", type="password")
15
+ if token_input:
16
+ st.session_state["hf_token"] = token_input
17
+ st.write("Token saved.")
18
+ else:
19
+ st.write("Token already saved.")
20
+
21
+ # Ensure token is valid before proceeding
22
+ if st.session_state["hf_token"] is None:
23
+ st.error("Please provide a valid token to proceed.")
24
+ st.stop()
25
+
26
+ # User Input for Space Name
27
+ space_name = st.text_input("Enter the name of the Hugging Face Space to update/create:")
28
+
29
+ # Code sections for `app.py` and `requirements.txt`
30
+ st.subheader("Provide your `app.py` Code")
31
+ app_code = st.text_area("Paste the code for `app.py` here:", height=200)
32
+
33
+ st.subheader("Provide your `requirements.txt` Code")
34
+ req_code = st.text_area("Paste the content for `requirements.txt` here:", height=100)
35
+
36
+ # Button to commit changes
37
+ if st.button("Commit Changes"):
38
+ if not st.session_state["hf_token"].strip():
39
+ st.error("Access token is required. Please enter a valid token.")
40
+ elif not space_name.strip():
41
+ st.error("Please enter a valid Space name.")
42
+ elif not app_code.strip() or not req_code.strip():
43
+ st.error("Both `app.py` and `requirements.txt` content are required.")
44
+ else:
45
+ # Initialize Hugging Face API
46
+ api = HfApi()
47
+
48
+ # Authenticate using the token
49
+ try:
50
+ token = st.session_state["hf_token"]
51
+ user_info = api.whoami(token=token)
52
+ st.success(f"Authenticated as {user_info['name']}")
53
+ except Exception as e:
54
+ st.error(f"Authentication failed: {e}")
55
+ st.stop()
56
+
57
+ try:
58
+ # Full Space name
59
+ space_full_name = f"{user_info['name']}/{space_name}"
60
+
61
+ # Check if Space exists or create a new one
62
+ try:
63
+ st.info(f"Checking if Space '{space_full_name}' exists...")
64
+ api.repo_info(repo_id=space_full_name, repo_type="space", token=token)
65
+ st.success(f"Space '{space_full_name}' exists. Updating files...")
66
+ except:
67
+ st.info(f"Space '{space_full_name}' does not exist. Creating new Space...")
68
+ create_repo(repo_id=space_name, repo_type="space", space_sdk="streamlit", token=token)
69
+
70
+ # Upload `app.py`
71
+ with open("app.py", "w") as app_file:
72
+ app_file.write(app_code)
73
+ upload_file(
74
+ path_or_fileobj="app.py",
75
+ path_in_repo="app.py",
76
+ repo_id=space_full_name,
77
+ repo_type="space",
78
+ token=token
79
+ )
80
+
81
+ # Upload `requirements.txt`
82
+ with open("requirements.txt", "w") as req_file:
83
+ req_file.write(req_code)
84
+ upload_file(
85
+ path_or_fileobj="requirements.txt",
86
+ path_in_repo="requirements.txt",
87
+ repo_id=space_full_name,
88
+ repo_type="space",
89
+ token=token
90
+ )
91
+
92
+ st.success(f"Files successfully updated in Space '{space_full_name}'!")
93
+
94
+ except Exception as e:
95
+ st.error(f"An error occurred: {e}")
96
+ finally:
97
+ # Clean up temporary files
98
+ if os.path.exists("app.py"):
99
+ os.remove("app.py")
100
+ if os.path.exists("requirements.txt"):
101
+ os.remove("requirements.txt")