Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from huggingface_hub import HfApi, create_repo, upload_file
|
3 |
+
from groq import Groq
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Set up API keys
|
7 |
+
hf_api_key = st.secrets["hf_token"]
|
8 |
+
groq_api_key = st.secrets["devassistapi"]
|
9 |
+
|
10 |
+
# Initialize Groq client
|
11 |
+
client = Groq(api_key=groq_api_key)
|
12 |
+
|
13 |
+
# App title
|
14 |
+
st.title("HF DevAssist - Create and Manage Hugging Face Spaces")
|
15 |
+
|
16 |
+
# Authenticate Hugging Face token
|
17 |
+
st.subheader("Authenticate with Hugging Face")
|
18 |
+
if not hf_api_key:
|
19 |
+
st.error("Hugging Face API key is missing. Add it to secrets.")
|
20 |
+
st.stop()
|
21 |
+
|
22 |
+
api = HfApi()
|
23 |
+
try:
|
24 |
+
user_info = api.whoami(token=hf_api_key)
|
25 |
+
st.success(f"Authenticated as {user_info['name']}")
|
26 |
+
except Exception as e:
|
27 |
+
st.error(f"Authentication failed: {e}")
|
28 |
+
st.stop()
|
29 |
+
|
30 |
+
# User Input for Space creation
|
31 |
+
st.subheader("Describe Your App")
|
32 |
+
space_name = st.text_input("Enter the name for the new or existing Hugging Face Space:")
|
33 |
+
app_description = st.text_area("Describe the app you want to create:", height=150)
|
34 |
+
|
35 |
+
if st.button("Generate and Deploy App"):
|
36 |
+
if not space_name.strip():
|
37 |
+
st.error("Space name cannot be empty.")
|
38 |
+
elif not app_description.strip():
|
39 |
+
st.error("App description cannot be empty.")
|
40 |
+
else:
|
41 |
+
try:
|
42 |
+
# Generate app code using Groq
|
43 |
+
st.info("Generating app code using Groq API...")
|
44 |
+
chat_completion = client.chat.completions.create(
|
45 |
+
messages=[
|
46 |
+
{
|
47 |
+
"role": "user",
|
48 |
+
"content": (
|
49 |
+
f"Generate a Streamlit app based on this description: "
|
50 |
+
f"'{app_description}'"
|
51 |
+
),
|
52 |
+
}
|
53 |
+
],
|
54 |
+
model="llama-3.3-70b-versatile",
|
55 |
+
)
|
56 |
+
generated_code = chat_completion.choices[0].message.content
|
57 |
+
|
58 |
+
# Split generated code into `app.py` and `requirements.txt`
|
59 |
+
app_code = generated_code.split("### requirements.txt")[0].strip()
|
60 |
+
req_code = generated_code.split("### requirements.txt")[1].strip()
|
61 |
+
|
62 |
+
# Create or update Hugging Face Space
|
63 |
+
space_full_name = f"{user_info['name']}/{space_name}"
|
64 |
+
try:
|
65 |
+
st.info(f"Checking if Space '{space_full_name}' exists...")
|
66 |
+
api.repo_info(repo_id=space_full_name, repo_type="space", token=hf_api_key)
|
67 |
+
st.success(f"Space '{space_full_name}' exists. Updating files...")
|
68 |
+
except:
|
69 |
+
st.info(f"Space '{space_full_name}' does not exist. Creating new Space...")
|
70 |
+
create_repo(repo_id=space_name, repo_type="space", space_sdk="streamlit", token=hf_api_key)
|
71 |
+
|
72 |
+
# Upload `app.py`
|
73 |
+
with open("app.py", "w") as app_file:
|
74 |
+
app_file.write(app_code)
|
75 |
+
upload_file(
|
76 |
+
path_or_fileobj="app.py",
|
77 |
+
path_in_repo="app.py",
|
78 |
+
repo_id=space_full_name,
|
79 |
+
repo_type="space",
|
80 |
+
token=hf_api_key,
|
81 |
+
)
|
82 |
+
|
83 |
+
# Upload `requirements.txt`
|
84 |
+
with open("requirements.txt", "w") as req_file:
|
85 |
+
req_file.write(req_code)
|
86 |
+
upload_file(
|
87 |
+
path_or_fileobj="requirements.txt",
|
88 |
+
path_in_repo="requirements.txt",
|
89 |
+
repo_id=space_full_name,
|
90 |
+
repo_type="space",
|
91 |
+
token=hf_api_key,
|
92 |
+
)
|
93 |
+
|
94 |
+
st.success(f"App successfully deployed in Space '{space_full_name}'!")
|
95 |
+
|
96 |
+
except Exception as e:
|
97 |
+
st.error(f"An error occurred: {e}")
|
98 |
+
finally:
|
99 |
+
# Clean up temporary files
|
100 |
+
if os.path.exists("app.py"):
|
101 |
+
os.remove("app.py")
|
102 |
+
if os.path.exists("requirements.txt"):
|
103 |
+
os.remove("requirements.txt")
|