Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
def google_search(query: str, num_results: int = 5) -> str:
|
| 4 |
-
""
|
| 5 |
-
API_KEY = "GOOGLE_API_KEY" # aus Google Cloud Console
|
| 6 |
CSE_CX = "77f1602c0ff764edb"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
resp = requests.get(
|
| 10 |
"https://www.googleapis.com/customsearch/v1",
|
|
@@ -12,7 +15,7 @@ def google_search(query: str, num_results: int = 5) -> str:
|
|
| 12 |
"key": API_KEY,
|
| 13 |
"cx": CSE_CX,
|
| 14 |
"q": query,
|
| 15 |
-
"num": min(num_results, 10),
|
| 16 |
},
|
| 17 |
timeout=8,
|
| 18 |
)
|
|
@@ -28,9 +31,25 @@ def google_search(query: str, num_results: int = 5) -> str:
|
|
| 28 |
title = item.get("title", "")
|
| 29 |
link = item.get("link", "")
|
| 30 |
snippet = item.get("snippet", "").replace("\n", " ")
|
| 31 |
-
snippets.append(f"
|
| 32 |
|
| 33 |
return "\n\n---\n\n".join(snippets)
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
-
return f"Google-Suchfehler: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
def google_search(query: str, num_results: int = 5) -> str:
|
| 5 |
+
API_KEY = "DEIN_API_KEY" # Hier API Key einfügen
|
|
|
|
| 6 |
CSE_CX = "77f1602c0ff764edb"
|
| 7 |
|
| 8 |
+
if not query.strip():
|
| 9 |
+
return "Bitte gib einen Suchbegriff ein."
|
| 10 |
+
|
| 11 |
try:
|
| 12 |
resp = requests.get(
|
| 13 |
"https://www.googleapis.com/customsearch/v1",
|
|
|
|
| 15 |
"key": API_KEY,
|
| 16 |
"cx": CSE_CX,
|
| 17 |
"q": query,
|
| 18 |
+
"num": max(1, min(int(num_results), 10)),
|
| 19 |
},
|
| 20 |
timeout=8,
|
| 21 |
)
|
|
|
|
| 31 |
title = item.get("title", "")
|
| 32 |
link = item.get("link", "")
|
| 33 |
snippet = item.get("snippet", "").replace("\n", " ")
|
| 34 |
+
snippets.append(f"### {title}\nURL: {link}\n\n{snippet}")
|
| 35 |
|
| 36 |
return "\n\n---\n\n".join(snippets)
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"Google-Suchfehler: {str(e)}"
|
| 40 |
+
|
| 41 |
+
# Gradio Interface Setup
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=google_search,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Textbox(label="Suchanfrage", placeholder="Wonach suchst du?"),
|
| 46 |
+
gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Anzahl Ergebnisse")
|
| 47 |
+
],
|
| 48 |
+
outputs=gr.Markdown(label="Ergebnisse"),
|
| 49 |
+
title="Google Custom Search UI",
|
| 50 |
+
description="Gib einen Begriff ein, um die Google API abzufragen.",
|
| 51 |
+
allow_flagging="never"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|