chthees commited on
Commit
5f509da
·
verified ·
1 Parent(s): 5b2c980

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -37
app.py CHANGED
@@ -2,45 +2,49 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import requests
4
 
5
- # Funktion, um Kontext von Wikipedia zu holen
6
- def get_wikipedia_summary(query):
7
- try:
8
- # Wir nutzen die öffentliche Wikipedia API
9
- response = requests.get(
10
- "https://de.wikipedia.org/w/api.php",
11
- params={
12
- "action": "query",
13
- "format": "json",
14
- "list": "search",
15
- "srsearch": query,
16
- "srlimit": 1
17
- }
18
- ).json()
19
-
20
- if not response["query"]["search"]:
21
- return None
22
-
23
- page_id = response["query"]["search"][0]["pageid"]
24
-
25
- # Details zur Seite holen
26
- details = requests.get(
27
- "https://de.wikipedia.org/w/api.php",
28
- params={
29
- "action": "query",
30
- "format": "json",
31
- "prop": "extracts",
32
- "pageids": page_id,
33
- "explaintext": True,
34
- "exintro": True,
35
- "exsentences": 7 # Nur die ersten 7 Sätze
36
- }
37
- ).json()
38
-
39
- page = details["query"]["pages"][str(page_id)]
40
- return page["extract"]
41
- except Exception as e:
42
  return None
43
 
 
 
 
 
 
44
  def respond(
45
  message,
46
  history: list[dict[str, str]],
 
2
  from huggingface_hub import InferenceClient
3
  import requests
4
 
5
+ def wiki_info(frage: str) -> str:
6
+ search_url = "https://de.wikipedia.org/w/api.php"
7
+ search_params = {
8
+ "action": "query",
9
+ "list": "search",
10
+ "srsearch": frage,
11
+ "format": "json"
12
+ }
13
+
14
+ r = requests.get(search_url, params=search_params)
15
+ data = r.json()
16
+
17
+ suchergebnisse = data.get("query", {}).get("search", [])
18
+ if not suchergebnisse:
19
+ return None
20
+
21
+ pageid = suchergebnisse[0]["pageid"]
22
+
23
+ # 2. Volltext laden
24
+ page_url = "https://de.wikipedia.org/w/api.php"
25
+ page_params = {
26
+ "action": "query",
27
+ "prop": "extracts",
28
+ "pageids": pageid,
29
+ "explaintext": 1,
30
+ "format": "json"
31
+ }
32
+
33
+ r2 = requests.get(page_url, params=page_params)
34
+ page_data = r2.json()
35
+
36
+ pages = page_data.get("query", {}).get("pages", {})
37
+ page = pages.get(str(pageid), {})
38
+
39
+ text = page.get("extract")
40
+ if not text:
 
41
  return None
42
 
43
+ saetze = re.split(r'(?<=[.!?]) +', text)
44
+ gekuerzt = " ".join(saetze[:10])
45
+
46
+ return gekuerzt
47
+
48
  def respond(
49
  message,
50
  history: list[dict[str, str]],