engrharis commited on
Commit
b319409
·
verified ·
1 Parent(s): 50fff8f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import HfApi, create_repo, upload_file
3
+ import os
4
+
5
+ # Global variable for storing the token (persistent while app runs)
6
+ hf_token = None
7
+
8
+ def save_token(token):
9
+ global hf_token
10
+ hf_token = token.strip()
11
+ return f"Token saved! You can now manage your Spaces."
12
+
13
+ def manage_space(space_name, app_code, req_code):
14
+ global hf_token
15
+ if not hf_token:
16
+ return "Please provide a valid Hugging Face token first."
17
+
18
+ if not space_name.strip():
19
+ return "Error: Space name is required."
20
+ if not app_code.strip() or not req_code.strip():
21
+ return "Error: Both `app.py` and `requirements.txt` code are required."
22
+
23
+ api = HfApi()
24
+ try:
25
+ # Authenticate using the token
26
+ user_info = api.whoami(token=hf_token)
27
+ space_full_name = f"{user_info['name']}/{space_name.strip()}"
28
+
29
+ # Check if Space exists or create a new one
30
+ try:
31
+ api.repo_info(repo_id=space_full_name, repo_type="space", token=hf_token)
32
+ message = f"Space '{space_full_name}' exists. Updating files..."
33
+ except:
34
+ create_repo(repo_id=space_name.strip(), repo_type="space", space_sdk="streamlit", token=hf_token)
35
+ message = f"Space '{space_full_name}' did not exist. Created a new Space."
36
+
37
+ # Upload `app.py`
38
+ with open("app.py", "w") as app_file:
39
+ app_file.write(app_code.strip())
40
+ upload_file(
41
+ path_or_fileobj="app.py",
42
+ path_in_repo="app.py",
43
+ repo_id=space_full_name,
44
+ repo_type="space",
45
+ token=hf_token
46
+ )
47
+
48
+ # Upload `requirements.txt`
49
+ with open("requirements.txt", "w") as req_file:
50
+ req_file.write(req_code.strip())
51
+ upload_file(
52
+ path_or_fileobj="requirements.txt",
53
+ path_in_repo="requirements.txt",
54
+ repo_id=space_full_name,
55
+ repo_type="space",
56
+ token=hf_token
57
+ )
58
+
59
+ # Clean up temporary files
60
+ os.remove("app.py")
61
+ os.remove("requirements.txt")
62
+
63
+ return f"{message}\nFiles successfully updated in Space '{space_full_name}'!"
64
+
65
+ except Exception as e:
66
+ return f"An error occurred: {e}"
67
+
68
+ # Gradio interface
69
+ token_input = gr.Textbox(label="Hugging Face Token", type="password", placeholder="Enter your token here")
70
+ save_token_button = gr.Button("Save Token")
71
+
72
+ space_name_input = gr.Textbox(label="Space Name", placeholder="Enter the name of your Space")
73
+ app_code_input = gr.Textbox(label="app.py Code", lines=15, placeholder="Paste your app.py code here")
74
+ req_code_input = gr.Textbox(label="requirements.txt Code", lines=5, placeholder="Paste your requirements.txt content here")
75
+ manage_button = gr.Button("Commit Changes")
76
+ output = gr.Textbox(label="Output", lines=5)
77
+
78
+ save_token_interface = gr.Interface(fn=save_token, inputs=token_input, outputs=output)
79
+ manage_space_interface = gr.Interface(
80
+ fn=manage_space,
81
+ inputs=[space_name_input, app_code_input, req_code_input],
82
+ outputs=output
83
+ )
84
+
85
+ app = gr.Blocks()
86
+
87
+ with app:
88
+ with gr.Row():
89
+ gr.Markdown("# Hugging Face Helper")
90
+ with gr.Box():
91
+ gr.Markdown("### Step 1: Save Your Hugging Face Token")
92
+ token_input.render()
93
+ save_token_button.render()
94
+ save_token_interface.render()
95
+
96
+ with gr.Box():
97
+ gr.Markdown("### Step 2: Manage Your Hugging Face Space")
98
+ space_name_input.render()
99
+ app_code_input.render()
100
+ req_code_input.render()
101
+ manage_button.render()
102
+ manage_space_interface.render()
103
+
104
+ # Launch the Gradio app
105
+ app.launch()