Spaces:
Paused

mgokg commited on
Commit
11e1654
·
verified ·
1 Parent(s): d5a29f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -85
app.py CHANGED
@@ -1,48 +1,19 @@
1
  import gradio as gr
2
  from litellm import completion
3
  import os
4
- import requests
5
 
6
  # Puter API Konfiguration
7
  PUTER_TOKEN = os.getenv("PUTER_API_KEY")
8
  PUTER_API_BASE = "https://api.puter.com/puterai/openai/v1/"
9
 
10
- def google_search(message):
11
- """Perform a web search using Google Custom Search API."""
12
- messages=message
13
- try:
14
 
15
- resp = requests.get(
16
- f"https://cse.google.com/cse?cx=77f1602c0ff764edb&q=test",
17
- )
18
- resp.raise_for_status()
19
- #data = resp.json()
20
- return resp
21
- return data
22
- items = data.get("items", [])
23
- if not items:
24
- return "Keine Suchergebnisse gefunden."
25
- snippets = []
26
- for item in items[:num_results]:
27
- title = item.get("title", "")
28
- link = item.get("link", "")
29
- snippet = item.get("snippet", "").replace("\n", " ")
30
- snippets.append(f"**{title}**\n{link}\n{snippet}")
31
- return "\n\n---\n\n".join(snippets)
32
- except Exception as e:
33
- return f"Google-Suchfehler: {str(e)}"
34
-
35
- """
36
  def respond(message, history, model):
37
  if history is None:
38
  history = []
39
-
40
  messages = [{"role": "user", "content": message}]
41
- tools = [{"type": "web_search"}]
42
  try:
43
  response = completion(
44
  model=f"openai/{model}",
45
- #tools = tools,
46
  messages=messages,
47
  api_key=PUTER_TOKEN,
48
  api_base=PUTER_API_BASE,
@@ -50,94 +21,78 @@ def respond(message, history, model):
50
  assistant_reply = response.choices[0].message.content
51
  except Exception as e:
52
  assistant_reply = f"**Fehler:** {str(e)}"
53
-
54
  history.append((message, assistant_reply))
55
 
56
  # Render as Markdown chat log
57
  chat_md = "\n\n".join(
58
- [f"** User:** {u}\n\n** Assistant:** {a}" for u, a in history]
59
  )
60
-
61
  return chat_md, history, ""
62
- """
 
63
  css = """
64
- :root {
65
- color-scheme: dark;
66
- }
67
- body, .gradio-container {
68
- background: #0f1115 !important;
69
- color: #e6e6e6 !important;
70
- font-family: 'Inter', system-ui, -apple-system, sans-serif;
71
  }
72
- #title {
73
- font-size: 1.8rem;
74
- font-weight: 700;
75
- margin-bottom: 0.4rem;
76
  }
77
- #subtitle {
78
- color: #b3b3b3;
79
- margin-bottom: 1.2rem;
80
  }
81
- .gr-box, .gr-input, .gr-textbox, .gr-dropdown {
82
- background: #151823 !important;
83
- border: 1px solid #2a2f3a !important;
84
- color: #e6e6e6 !important;
85
  border-radius: 12px !important;
86
  }
87
- .gr-button {
88
- background: linear-gradient(135deg, #6c5ce7, #00d2ff) !important;
89
- color: #fff !important;
90
- border-radius: 12px !important;
91
- border: none !important;
92
  font-weight: 600 !important;
93
  }
94
- .gr-button:hover {
95
- filter: brightness(1.08);
96
  }
97
- #output-markdown {
98
- background: #11141b !important;
99
- border: 1px solid #2a2f3a !important;
100
- border-radius: 16px !important;
101
- padding: 16px !important;
102
  }
103
  """
104
 
105
  with gr.Blocks() as demo:
106
  gr.Markdown("# puter.js in Python via LiteLLM", elem_id="title")
107
-
108
-
109
  with gr.Row():
110
  model = gr.Dropdown(
111
  choices=["gpt-4o-mini", "gpt-5.2-codex", "claude-sonnet-4", "gemini-2.5-flash"],
112
  value="gpt-4o-mini",
113
  label="Modell auswählen"
114
  )
115
-
116
- output = gr.Markdown(value="", elem_id="output-markdown")
117
-
118
  with gr.Row():
119
  msg = gr.Textbox(
120
  label="Deine Nachricht",
121
  placeholder="Schreibe hier...",
122
  lines=2,
123
- scale=4
124
  )
125
- send = gr.Button("Senden", scale=1)
126
 
127
- state = gr.State([])
128
-
129
- send.click(
130
- fn=google_search,
131
- #fn=respond,
132
- inputs=[msg],
133
- outputs=[output]
134
- )
135
- msg.submit(
136
- fn=google_search,
137
- #fn=respond,
138
- inputs=[msg],
139
- outputs=[output]
140
  )
141
 
142
- if __name__ == "__main__":
143
- demo.launch(css=css)
 
1
  import gradio as gr
2
  from litellm import completion
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def respond(message, history, model):
11
  if history is None:
12
  history = []
 
13
  messages = [{"role": "user", "content": message}]
 
14
  try:
15
  response = completion(
16
  model=f"openai/{model}",
 
17
  messages=messages,
18
  api_key=PUTER_TOKEN,
19
  api_base=PUTER_API_BASE,
 
21
  assistant_reply = response.choices[0].message.content
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 {
36
+ background: #0f1115 !important;
37
+ color: #e6e6e6 !important;
38
+ font-family: 'Inter', system-ui, -apple-system, sans-serif;
 
 
39
  }
40
+ #title {
41
+ font-size: 1.8rem;
42
+ font-weight: 700;
43
+ margin-bottom: 0.4rem;
44
  }
45
+ #subtitle {
46
+ color: #b3b3b3;
47
+ margin-bottom: 1.2rem;
48
  }
49
+ .gr-box, .gr-input, .gr-textbox, .gr-dropdown {
50
+ background: #151823 !important;
51
+ border: 1px solid #2a2f3a !important;
52
+ color: #e6e6e6 !important;
53
  border-radius: 12px !important;
54
  }
55
+ .gr-button {
56
+ background: linear-gradient(135deg, #6c5ce7, #00d2ff) !important;
57
+ color: #fff !important;
58
+ border-radius: 12px !important;
59
+ border: none !important;
60
  font-weight: 600 !important;
61
  }
62
+ .gr-button:hover {
63
+ filter: brightness(1.08);
64
  }
65
+ #output-markdown {
66
+ background: #11141b !important;
67
+ border: 1px solid #2a2f3a !important;
68
+ border-radius: 16px !important;
69
+ padding: 16px !important;
70
  }
71
  """
72
 
73
  with gr.Blocks() as demo:
74
  gr.Markdown("# puter.js in Python via LiteLLM", elem_id="title")
 
 
75
  with gr.Row():
76
  model = gr.Dropdown(
77
  choices=["gpt-4o-mini", "gpt-5.2-codex", "claude-sonnet-4", "gemini-2.5-flash"],
78
  value="gpt-4o-mini",
79
  label="Modell auswählen"
80
  )
81
+ output = gr.Markdown(value="", elem_id="output-markdown")
 
 
82
  with gr.Row():
83
  msg = gr.Textbox(
84
  label="Deine Nachricht",
85
  placeholder="Schreibe hier...",
86
  lines=2,
 
87
  )
88
+ submit_btn = gr.Button("Senden")
89
 
90
+ # Event handler
91
+ submit_btn.click(
92
+ fn=respond,
93
+ inputs=[msg, output, model],
94
+ outputs=[output, output, msg]
 
 
 
 
 
 
 
 
95
  )
96
 
97
+ # Launch the app
98
+ demo.launch()