Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
import os
|
| 4 |
|
| 5 |
# Puter API Konfiguration
|
| 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 = []
|
| 12 |
messages = [{"role": "user", "content": message}]
|
| 13 |
try:
|
| 14 |
-
response =
|
| 15 |
-
model=
|
| 16 |
messages=messages,
|
| 17 |
-
api_key=PUTER_TOKEN,
|
| 18 |
-
api_base=PUTER_API_BASE,
|
| 19 |
)
|
| 20 |
assistant_reply = response.choices[0].message.content
|
| 21 |
except Exception as e:
|
|
@@ -66,34 +69,25 @@ 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
|
| 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],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
# Puter API Konfiguration
|
| 6 |
PUTER_TOKEN = os.getenv("PUTER_API_KEY")
|
| 7 |
PUTER_API_BASE = "https://api.puter.com/puterai/openai/v1/"
|
| 8 |
|
| 9 |
+
client = OpenAI(
|
| 10 |
+
api_key=PUTER_TOKEN,
|
| 11 |
+
base_url=PUTER_API_BASE,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
def respond(message, history, model):
|
| 15 |
if history is None:
|
| 16 |
history = []
|
| 17 |
messages = [{"role": "user", "content": message}]
|
| 18 |
try:
|
| 19 |
+
response = client.chat.completions.create(
|
| 20 |
+
model=model,
|
| 21 |
messages=messages,
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
assistant_reply = response.choices[0].message.content
|
| 24 |
except Exception as e:
|
|
|
|
| 69 |
}
|
| 70 |
"""
|
| 71 |
|
|
|
|
| 72 |
with gr.Blocks(css=css) as demo:
|
| 73 |
+
gr.Markdown("# puter.js in Python via OpenAI SDK", elem_id="title")
|
|
|
|
|
|
|
| 74 |
state = gr.State([])
|
|
|
|
| 75 |
with gr.Row():
|
| 76 |
output = gr.Markdown(value="", elem_id="output-markdown")
|
|
|
|
| 77 |
with gr.Row():
|
| 78 |
model = gr.Dropdown(
|
| 79 |
choices=["gpt-4o-mini", "gpt-5.2-codex", "claude-sonnet-4", "gemini-2.5-flash"],
|
| 80 |
value="gpt-4o-mini",
|
| 81 |
label="Modell auswählen"
|
| 82 |
)
|
|
|
|
| 83 |
with gr.Row():
|
| 84 |
msg = gr.Textbox(
|
| 85 |
label="Deine Nachricht",
|
| 86 |
placeholder="Schreibe hier...",
|
| 87 |
lines=2,
|
| 88 |
)
|
|
|
|
| 89 |
with gr.Row():
|
| 90 |
submit_btn = gr.Button("Senden")
|
|
|
|
|
|
|
| 91 |
submit_btn.click(
|
| 92 |
fn=respond,
|
| 93 |
inputs=[msg, state, model],
|