Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -67,7 +67,7 @@ def handle_message(user_msg: str, state: Dict[str, Any]) -> Tuple[str, Dict[str,
|
|
67 |
logs.append("⚠️ Failed to parse assistant JSON.\n" + resp.text)
|
68 |
return "⚠️ Parsing error. Check logs.", state
|
69 |
|
70 |
-
#
|
71 |
if not state["created"]:
|
72 |
full_repo = f"{state['hf_username']}/{state['repo_name']}"
|
73 |
create_repo(
|
@@ -83,7 +83,7 @@ def handle_message(user_msg: str, state: Dict[str, Any]) -> Tuple[str, Dict[str,
|
|
83 |
"embed_url": f"https://huggingface.co/spaces/{full_repo}",
|
84 |
})
|
85 |
|
86 |
-
#
|
87 |
if files:
|
88 |
logs.append(f"Writing {len(files)} file(s): {list(files)}")
|
89 |
for relpath, content in files.items():
|
@@ -92,7 +92,7 @@ def handle_message(user_msg: str, state: Dict[str, Any]) -> Tuple[str, Dict[str,
|
|
92 |
with open(dest, "w", encoding="utf‑8") as fp:
|
93 |
fp.write(content)
|
94 |
|
95 |
-
#
|
96 |
HfApi(token=state["hf_token"]).upload_folder(
|
97 |
folder_path=state["local_path"],
|
98 |
repo_id=state["repo_id"],
|
@@ -103,34 +103,35 @@ def handle_message(user_msg: str, state: Dict[str, Any]) -> Tuple[str, Dict[str,
|
|
103 |
|
104 |
with gr.Blocks(title="Gemini → HF Space scaffolder") as demo:
|
105 |
with gr.Row():
|
106 |
-
login_btn
|
107 |
logout_value="Logout ({username})",
|
108 |
variant="huggingface",
|
109 |
size="lg"
|
110 |
)
|
111 |
login_btn.activate()
|
112 |
-
status_md
|
113 |
|
114 |
-
|
|
|
115 |
return "*Not logged in.*" if profile is None else f"Logged in as **{profile.username}**"
|
116 |
|
117 |
demo.load(fn=show_profile, inputs=[login_btn], outputs=status_md)
|
118 |
|
119 |
with gr.Row():
|
120 |
with gr.Column(scale=1):
|
121 |
-
gemini_key
|
122 |
-
hf_user
|
123 |
-
repo_name
|
124 |
-
session_id
|
125 |
-
start_btn
|
126 |
with gr.Column(scale=3):
|
127 |
-
chatbot
|
128 |
-
logs_display
|
129 |
-
preview_iframe
|
130 |
-
user_msg
|
131 |
-
send_btn
|
132 |
|
133 |
-
# on_start needs the
|
134 |
def on_start(
|
135 |
evt,
|
136 |
gemini_key: str,
|
@@ -151,25 +152,8 @@ with gr.Blocks(title="Gemini → HF Space scaffolder") as demo:
|
|
151 |
outputs=[session_id, logs_display, preview_iframe, send_btn],
|
152 |
)
|
153 |
|
154 |
-
#
|
155 |
-
def
|
156 |
-
evt,
|
157 |
-
msg: str,
|
158 |
-
chat_history: list[dict],
|
159 |
-
sess_id: str
|
160 |
-
):
|
161 |
-
return _handle_send(msg, chat_history, sess_id)
|
162 |
-
|
163 |
-
# on_send for the textbox (no evt)
|
164 |
-
def on_send_submit(
|
165 |
-
msg: str,
|
166 |
-
chat_history: list[dict],
|
167 |
-
sess_id: str
|
168 |
-
):
|
169 |
-
return _handle_send(msg, chat_history, sess_id)
|
170 |
-
|
171 |
-
# shared send logic
|
172 |
-
def _handle_send(
|
173 |
msg: str,
|
174 |
chat_history: list[dict],
|
175 |
sess_id: str
|
@@ -180,8 +164,8 @@ with gr.Blocks(title="Gemini → HF Space scaffolder") as demo:
|
|
180 |
state_store[sess_id] = new_state
|
181 |
|
182 |
history = chat_history or []
|
183 |
-
history.append({"role": "user",
|
184 |
-
history.append({"role": "assistant",
|
185 |
|
186 |
logs = "\n".join(new_state["logs"])
|
187 |
embed = ""
|
@@ -190,12 +174,12 @@ with gr.Blocks(title="Gemini → HF Space scaffolder") as demo:
|
|
190 |
return history, sess_id, logs, embed
|
191 |
|
192 |
send_btn.click(
|
193 |
-
|
194 |
inputs=[user_msg, chatbot, session_id],
|
195 |
outputs=[chatbot, session_id, logs_display, preview_iframe],
|
196 |
)
|
197 |
user_msg.submit(
|
198 |
-
|
199 |
inputs=[user_msg, chatbot, session_id],
|
200 |
outputs=[chatbot, session_id, logs_display, preview_iframe],
|
201 |
)
|
|
|
67 |
logs.append("⚠️ Failed to parse assistant JSON.\n" + resp.text)
|
68 |
return "⚠️ Parsing error. Check logs.", state
|
69 |
|
70 |
+
# On first round, create the Space repo
|
71 |
if not state["created"]:
|
72 |
full_repo = f"{state['hf_username']}/{state['repo_name']}"
|
73 |
create_repo(
|
|
|
83 |
"embed_url": f"https://huggingface.co/spaces/{full_repo}",
|
84 |
})
|
85 |
|
86 |
+
# Write any returned files
|
87 |
if files:
|
88 |
logs.append(f"Writing {len(files)} file(s): {list(files)}")
|
89 |
for relpath, content in files.items():
|
|
|
92 |
with open(dest, "w", encoding="utf‑8") as fp:
|
93 |
fp.write(content)
|
94 |
|
95 |
+
# Push a snapshot
|
96 |
HfApi(token=state["hf_token"]).upload_folder(
|
97 |
folder_path=state["local_path"],
|
98 |
repo_id=state["repo_id"],
|
|
|
103 |
|
104 |
with gr.Blocks(title="Gemini → HF Space scaffolder") as demo:
|
105 |
with gr.Row():
|
106 |
+
login_btn = gr.LoginButton(
|
107 |
logout_value="Logout ({username})",
|
108 |
variant="huggingface",
|
109 |
size="lg"
|
110 |
)
|
111 |
login_btn.activate()
|
112 |
+
status_md = gr.Markdown("Not logged in")
|
113 |
|
114 |
+
# Accept the event-plus-profile so that Gradio.load injects correctly
|
115 |
+
def show_profile(evt, profile: gr.OAuthProfile | None):
|
116 |
return "*Not logged in.*" if profile is None else f"Logged in as **{profile.username}**"
|
117 |
|
118 |
demo.load(fn=show_profile, inputs=[login_btn], outputs=status_md)
|
119 |
|
120 |
with gr.Row():
|
121 |
with gr.Column(scale=1):
|
122 |
+
gemini_key = gr.Textbox(label="Gemini API Key", type="password")
|
123 |
+
hf_user = gr.Textbox(label="HF Username")
|
124 |
+
repo_name = gr.Textbox(label="New App (repo) name")
|
125 |
+
session_id = gr.Textbox(value="", visible=False)
|
126 |
+
start_btn = gr.Button("Start a new app")
|
127 |
with gr.Column(scale=3):
|
128 |
+
chatbot = gr.Chatbot(type="messages", value=[])
|
129 |
+
logs_display = gr.Textbox(label="Operation Logs", interactive=False, lines=8)
|
130 |
+
preview_iframe= gr.HTML("<p>No deployed app yet.</p>")
|
131 |
+
user_msg = gr.Textbox(label="Your message")
|
132 |
+
send_btn = gr.Button("Send", interactive=False)
|
133 |
|
134 |
+
# on_start needs the event param
|
135 |
def on_start(
|
136 |
evt,
|
137 |
gemini_key: str,
|
|
|
152 |
outputs=[session_id, logs_display, preview_iframe, send_btn],
|
153 |
)
|
154 |
|
155 |
+
# Single handler for both button click and textbox submit (3 args)
|
156 |
+
def on_send(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
msg: str,
|
158 |
chat_history: list[dict],
|
159 |
sess_id: str
|
|
|
164 |
state_store[sess_id] = new_state
|
165 |
|
166 |
history = chat_history or []
|
167 |
+
history.append({"role": "user", "content": msg})
|
168 |
+
history.append({"role": "assistant","content": reply})
|
169 |
|
170 |
logs = "\n".join(new_state["logs"])
|
171 |
embed = ""
|
|
|
174 |
return history, sess_id, logs, embed
|
175 |
|
176 |
send_btn.click(
|
177 |
+
on_send,
|
178 |
inputs=[user_msg, chatbot, session_id],
|
179 |
outputs=[chatbot, session_id, logs_display, preview_iframe],
|
180 |
)
|
181 |
user_msg.submit(
|
182 |
+
on_send,
|
183 |
inputs=[user_msg, chatbot, session_id],
|
184 |
outputs=[chatbot, session_id, logs_display, preview_iframe],
|
185 |
)
|