Spaces:
Paused

mgokg commited on
Commit
e1e8473
·
verified ·
1 Parent(s): ba38ee0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -87
app.py CHANGED
@@ -1,102 +1,40 @@
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:
25
- assistant_reply = f"**Fehler:** {str(e)}"
26
- history.append((message, assistant_reply))
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
- css = """
33
- :root { color-scheme: dark; }
34
- body, .gradio-container {
35
- background: #0f1115 !important;
36
- color: #e6e6e6 !important;
37
- font-family: 'Inter', system-ui, -apple-system, sans-serif;
38
- }
39
- #title {
40
- font-size: 1.8rem;
41
- font-weight: 700;
42
- margin-bottom: 0.4rem;
43
- }
44
- #subtitle {
45
- color: #b3b3b3;
46
- margin-bottom: 1.2rem;
47
- }
48
- .gr-box, .gr-input, .gr-textbox, .gr-dropdown {
49
- background: #151823 !important;
50
- border: 1px solid #2a2f3a !important;
51
- color: #e6e6e6 !important;
52
- border-radius: 12px !important;
53
- }
54
- .gr-button {
55
- background: linear-gradient(135deg, #6c5ce7, #00d2ff) !important;
56
- color: #fff !important;
57
- border-radius: 12px !important;
58
- border: none !important;
59
- font-weight: 600 !important;
60
- }
61
- .gr-button:hover {
62
- filter: brightness(1.08);
63
- }
64
- #output-markdown {
65
- background: #11141b !important;
66
- border: 1px solid #2a2f3a !important;
67
- border-radius: 16px !important;
68
- padding: 16px !important;
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],
94
- outputs=[output, state, msg]
95
- )
96
- msg.submit(
97
- fn=respond,
98
- inputs=[msg, state, model],
99
- outputs=[output, state, msg]
100
- )
101
 
102
- demo.launch()
 
 
1
  import gradio as gr
2
+ from litellm import completion
3
  import os
4
 
5
  # Puter API Konfiguration
6
+ # Hol dir deinen Token aus den Browser-Entwicklertools (LocalStorage 'puter-auth-token')
7
+ # oder via Puter-Dashboard und füge ihn als Secret in Hugging Face ein.
8
+ PUTER_TOKEN = os.getenv("PUTER_API_KEY")
9
+ PUTER_API_BASE = "https://api.puter.com"
 
 
 
10
 
11
  def respond(message, history, model):
 
 
12
  messages = [{"role": "user", "content": message}]
13
+
14
  try:
15
+ response = completion(
16
+ model=f"openai/{model}", # LiteLLM nutzt openai-Format für Puter
17
  messages=messages,
18
+ api_key=PUTER_TOKEN,
19
+ api_base=PUTER_API_BASE,
20
  )
21
+ return response.choices[0].message.content
22
  except Exception as e:
23
+ return f"Fehler: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ # Gradio Interface
26
+ demo = gr.ChatInterface(
27
+ fn=respond,
28
+ additional_inputs=[
29
+ gr.Dropdown(
30
+ choices=["gpt-4o", "gpt-4-turbo", "claude-3-5-sonnet", "gemini-1.5-pro"],
31
+ value="gpt-4o",
 
 
32
  label="Modell auswählen"
33
  )
34
+ ],
35
+ title="Puter AI x Python",
36
+ description="Nutze Puter-LLMs direkt in Python via LiteLLM."
37
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ if __name__ == "__main__":
40
+ demo.launch()