Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -30,6 +30,22 @@ def fetch_logs(repo_id: str, level: str):
|
|
30 |
continue
|
31 |
return "\n".join(lines)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
# — CORE LOOP: send prompt & (iteratively) deploy —
|
34 |
|
35 |
def handle_user_message(
|
@@ -38,11 +54,13 @@ def handle_user_message(
|
|
38 |
sdk_choice: str,
|
39 |
gemini_api_key, grounding_enabled
|
40 |
):
|
41 |
-
#
|
42 |
if hf_profile is None or hf_token is None:
|
43 |
-
return history + [
|
44 |
|
45 |
genai_client = genai.Client(api_key=gemini_api_key)
|
|
|
|
|
46 |
chat = [{
|
47 |
"role":"system",
|
48 |
"content":(
|
@@ -51,19 +69,19 @@ def handle_user_message(
|
|
51 |
"If errors appear, fix them and return the full updated code."
|
52 |
)
|
53 |
}]
|
54 |
-
|
55 |
-
chat.append({"role": role, "content": msg})
|
56 |
|
57 |
filename = "app.py" if sdk_choice=="gradio" else "streamlit_app.py"
|
58 |
build_logs = run_logs = ""
|
59 |
|
60 |
for _ in range(5):
|
61 |
-
#
|
62 |
tools = []
|
63 |
if grounding_enabled:
|
64 |
tools.append(Tool(google_search=GoogleSearch()))
|
65 |
config = GenerateContentConfig(tools=tools, response_modalities=["TEXT"])
|
66 |
|
|
|
67 |
resp = genai_client.models.generate_content(
|
68 |
model="gemini-2.5-flash-preview-04-17",
|
69 |
contents=[c["content"] for c in chat],
|
@@ -92,12 +110,15 @@ def handle_user_message(
|
|
92 |
repo_type="space"
|
93 |
)
|
94 |
|
|
|
95 |
build_logs = fetch_logs(repo_id, "build")
|
96 |
run_logs = fetch_logs(repo_id, "run")
|
97 |
|
|
|
98 |
if "ERROR" not in build_logs.upper() and "ERROR" not in run_logs.upper():
|
99 |
break
|
100 |
|
|
|
101 |
chat.append({
|
102 |
"role":"user",
|
103 |
"content":(
|
@@ -108,9 +129,13 @@ def handle_user_message(
|
|
108 |
})
|
109 |
time.sleep(2)
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
114 |
|
115 |
# — GRADIO UI —
|
116 |
|
@@ -118,27 +143,31 @@ with gr.Blocks(title="HF Space Auto‑Builder (Gradio & Streamlit)") as demo:
|
|
118 |
with gr.Row():
|
119 |
with gr.Column(scale=1):
|
120 |
gr.Markdown("### Sidebar")
|
121 |
-
|
122 |
profile_state = gr.State()
|
123 |
token_state = gr.State()
|
124 |
|
125 |
login_btn = gr.LoginButton("huggingface", size="sm")
|
126 |
login_status = gr.Markdown("*Not logged in.*")
|
127 |
|
128 |
-
# capture profile
|
129 |
login_btn.click(
|
130 |
-
lambda profile, oauth_token: (profile, oauth_token),
|
|
|
131 |
outputs=[profile_state, token_state]
|
132 |
)
|
133 |
-
# update status text
|
134 |
login_btn.click(
|
135 |
-
lambda profile, oauth_token: f"Logged in as **{profile.username}**"
|
136 |
-
|
|
|
137 |
outputs=login_status
|
138 |
)
|
139 |
|
140 |
sdk_choice = gr.Radio(
|
141 |
-
["gradio","streamlit"],
|
|
|
|
|
142 |
)
|
143 |
api_key = gr.Textbox(label="Gemini API Key", type="password")
|
144 |
grounding = gr.Checkbox(label="Enable grounding", value=False)
|
|
|
30 |
continue
|
31 |
return "\n".join(lines)
|
32 |
|
33 |
+
# — UTILS —
|
34 |
+
|
35 |
+
def normalize_history(history):
|
36 |
+
"""
|
37 |
+
Take Chatbot 'messages' history (list of ChatMessage or dict)
|
38 |
+
and return list of dicts: {'role': ..., 'content': ...}
|
39 |
+
"""
|
40 |
+
out = []
|
41 |
+
for msg in history:
|
42 |
+
if isinstance(msg, dict):
|
43 |
+
out.append({"role": msg["role"], "content": msg["content"]})
|
44 |
+
else:
|
45 |
+
# ChatMessage namedtuple
|
46 |
+
out.append({"role": msg.role, "content": msg.content})
|
47 |
+
return out
|
48 |
+
|
49 |
# — CORE LOOP: send prompt & (iteratively) deploy —
|
50 |
|
51 |
def handle_user_message(
|
|
|
54 |
sdk_choice: str,
|
55 |
gemini_api_key, grounding_enabled
|
56 |
):
|
57 |
+
# must be logged in
|
58 |
if hf_profile is None or hf_token is None:
|
59 |
+
return history + [{"role":"assistant","content":"⚠️ Please log in first."}], "", "", "<p>No Space yet.</p>"
|
60 |
|
61 |
genai_client = genai.Client(api_key=gemini_api_key)
|
62 |
+
|
63 |
+
# build system + user chat
|
64 |
chat = [{
|
65 |
"role":"system",
|
66 |
"content":(
|
|
|
69 |
"If errors appear, fix them and return the full updated code."
|
70 |
)
|
71 |
}]
|
72 |
+
chat += normalize_history(history)
|
|
|
73 |
|
74 |
filename = "app.py" if sdk_choice=="gradio" else "streamlit_app.py"
|
75 |
build_logs = run_logs = ""
|
76 |
|
77 |
for _ in range(5):
|
78 |
+
# setup tools for grounding
|
79 |
tools = []
|
80 |
if grounding_enabled:
|
81 |
tools.append(Tool(google_search=GoogleSearch()))
|
82 |
config = GenerateContentConfig(tools=tools, response_modalities=["TEXT"])
|
83 |
|
84 |
+
# call Gemini
|
85 |
resp = genai_client.models.generate_content(
|
86 |
model="gemini-2.5-flash-preview-04-17",
|
87 |
contents=[c["content"] for c in chat],
|
|
|
110 |
repo_type="space"
|
111 |
)
|
112 |
|
113 |
+
# fetch logs
|
114 |
build_logs = fetch_logs(repo_id, "build")
|
115 |
run_logs = fetch_logs(repo_id, "run")
|
116 |
|
117 |
+
# done if no errors
|
118 |
if "ERROR" not in build_logs.upper() and "ERROR" not in run_logs.upper():
|
119 |
break
|
120 |
|
121 |
+
# else feed back
|
122 |
chat.append({
|
123 |
"role":"user",
|
124 |
"content":(
|
|
|
129 |
})
|
130 |
time.sleep(2)
|
131 |
|
132 |
+
# prepare return: a list of dicts for Chatbot(type="messages")
|
133 |
+
return (
|
134 |
+
[{"role":m["role"], "content":m["content"]} for m in chat if m["role"]!="system"],
|
135 |
+
build_logs,
|
136 |
+
run_logs,
|
137 |
+
f'<iframe src="https://huggingface.co/spaces/{repo_id}" width="100%" height="500px"></iframe>'
|
138 |
+
)
|
139 |
|
140 |
# — GRADIO UI —
|
141 |
|
|
|
143 |
with gr.Row():
|
144 |
with gr.Column(scale=1):
|
145 |
gr.Markdown("### Sidebar")
|
146 |
+
|
147 |
profile_state = gr.State()
|
148 |
token_state = gr.State()
|
149 |
|
150 |
login_btn = gr.LoginButton("huggingface", size="sm")
|
151 |
login_status = gr.Markdown("*Not logged in.*")
|
152 |
|
153 |
+
# capture the profile/token from the LoginButton
|
154 |
login_btn.click(
|
155 |
+
fn=lambda profile, oauth_token: (profile, oauth_token),
|
156 |
+
inputs=None,
|
157 |
outputs=[profile_state, token_state]
|
158 |
)
|
159 |
+
# update the status text once those states are set
|
160 |
login_btn.click(
|
161 |
+
fn=lambda profile, oauth_token: f"Logged in as **{profile.username}**"
|
162 |
+
if profile else "*Not logged in.*",
|
163 |
+
inputs=[profile_state, token_state],
|
164 |
outputs=login_status
|
165 |
)
|
166 |
|
167 |
sdk_choice = gr.Radio(
|
168 |
+
["gradio","streamlit"],
|
169 |
+
value="gradio",
|
170 |
+
label="SDK Template"
|
171 |
)
|
172 |
api_key = gr.Textbox(label="Gemini API Key", type="password")
|
173 |
grounding = gr.Checkbox(label="Enable grounding", value=False)
|