Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import os
|
|
| 6 |
PUTER_TOKEN = os.getenv("PUTER_API_KEY")
|
| 7 |
PUTER_API_BASE = "https://api.puter.com/puterai/openai/v1/"
|
| 8 |
|
| 9 |
-
|
| 10 |
def respond(message, history, model):
|
| 11 |
if history is None:
|
| 12 |
history = []
|
|
@@ -22,14 +21,11 @@ def respond(message, history, model):
|
|
| 22 |
except Exception as e:
|
| 23 |
assistant_reply = f"**Fehler:** {str(e)}"
|
| 24 |
history.append((message, assistant_reply))
|
| 25 |
-
|
| 26 |
-
# Render as Markdown chat log
|
| 27 |
chat_md = "\n\n".join(
|
| 28 |
[f"**User:** {u}\n\n**Assistant:** {a}" for u, a in history]
|
| 29 |
)
|
| 30 |
return chat_md, history, ""
|
| 31 |
|
| 32 |
-
|
| 33 |
css = """
|
| 34 |
:root { color-scheme: dark; }
|
| 35 |
body, .gradio-container {
|
|
@@ -70,39 +66,43 @@ body, .gradio-container {
|
|
| 70 |
}
|
| 71 |
"""
|
| 72 |
|
| 73 |
-
|
|
|
|
| 74 |
gr.Markdown("# puter.js in Python via LiteLLM", elem_id="title")
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
with gr.Row():
|
| 77 |
output = gr.Markdown(value="", elem_id="output-markdown")
|
| 78 |
-
|
| 79 |
with gr.Row():
|
| 80 |
model = gr.Dropdown(
|
| 81 |
choices=["gpt-4o-mini", "gpt-5.2-codex", "claude-sonnet-4", "gemini-2.5-flash"],
|
| 82 |
value="gpt-4o-mini",
|
| 83 |
label="Modell auswählen"
|
| 84 |
)
|
| 85 |
-
|
| 86 |
with gr.Row():
|
| 87 |
msg = gr.Textbox(
|
| 88 |
label="Deine Nachricht",
|
| 89 |
placeholder="Schreibe hier...",
|
| 90 |
lines=2,
|
| 91 |
)
|
|
|
|
| 92 |
with gr.Row():
|
| 93 |
submit_btn = gr.Button("Senden")
|
| 94 |
|
| 95 |
-
#
|
| 96 |
submit_btn.click(
|
| 97 |
fn=respond,
|
| 98 |
inputs=[msg, state, model],
|
| 99 |
outputs=[output, state, msg]
|
| 100 |
)
|
| 101 |
-
|
| 102 |
msg.submit(
|
| 103 |
fn=respond,
|
| 104 |
inputs=[msg, state, model],
|
| 105 |
outputs=[output, state, msg]
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
demo.launch(
|
|
|
|
| 6 |
PUTER_TOKEN = os.getenv("PUTER_API_KEY")
|
| 7 |
PUTER_API_BASE = "https://api.puter.com/puterai/openai/v1/"
|
| 8 |
|
|
|
|
| 9 |
def respond(message, history, model):
|
| 10 |
if history is None:
|
| 11 |
history = []
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
assistant_reply = f"**Fehler:** {str(e)}"
|
| 23 |
history.append((message, assistant_reply))
|
|
|
|
|
|
|
| 24 |
chat_md = "\n\n".join(
|
| 25 |
[f"**User:** {u}\n\n**Assistant:** {a}" for u, a in history]
|
| 26 |
)
|
| 27 |
return chat_md, history, ""
|
| 28 |
|
|
|
|
| 29 |
css = """
|
| 30 |
:root { color-scheme: dark; }
|
| 31 |
body, .gradio-container {
|
|
|
|
| 66 |
}
|
| 67 |
"""
|
| 68 |
|
| 69 |
+
# ✅ css passed to gr.Blocks, not demo.launch()
|
| 70 |
+
with gr.Blocks(css=css) as demo:
|
| 71 |
gr.Markdown("# puter.js in Python via LiteLLM", elem_id="title")
|
| 72 |
+
|
| 73 |
+
# ✅ state declared INSIDE gr.Blocks before it's referenced
|
| 74 |
+
state = gr.State([])
|
| 75 |
+
|
| 76 |
with gr.Row():
|
| 77 |
output = gr.Markdown(value="", elem_id="output-markdown")
|
| 78 |
+
|
| 79 |
with gr.Row():
|
| 80 |
model = gr.Dropdown(
|
| 81 |
choices=["gpt-4o-mini", "gpt-5.2-codex", "claude-sonnet-4", "gemini-2.5-flash"],
|
| 82 |
value="gpt-4o-mini",
|
| 83 |
label="Modell auswählen"
|
| 84 |
)
|
| 85 |
+
|
| 86 |
with gr.Row():
|
| 87 |
msg = gr.Textbox(
|
| 88 |
label="Deine Nachricht",
|
| 89 |
placeholder="Schreibe hier...",
|
| 90 |
lines=2,
|
| 91 |
)
|
| 92 |
+
|
| 93 |
with gr.Row():
|
| 94 |
submit_btn = gr.Button("Senden")
|
| 95 |
|
| 96 |
+
# ✅ state now exists when referenced here
|
| 97 |
submit_btn.click(
|
| 98 |
fn=respond,
|
| 99 |
inputs=[msg, state, model],
|
| 100 |
outputs=[output, state, msg]
|
| 101 |
)
|
|
|
|
| 102 |
msg.submit(
|
| 103 |
fn=respond,
|
| 104 |
inputs=[msg, state, model],
|
| 105 |
outputs=[output, state, msg]
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
demo.launch()
|