mgokg commited on
Commit
ae2ec3d
·
verified ·
1 Parent(s): ee76119

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -1
app.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  import os
4
  import json
5
  import google.generativeai as genai
 
6
 
7
  # Load environment variables
8
  genai.configure(api_key=os.environ["geminiapikey"])
@@ -19,6 +20,31 @@ custom_css = """
19
  }
20
  """
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def predict(prompt):
23
  # Create the model
24
  generation_config = {
@@ -55,7 +81,7 @@ with gr.Blocks(css=custom_css) as demo:
55
  button = gr.Button("Senden")
56
 
57
  # Connect the button to the function
58
- button.click(fn=predict, inputs=ort_input, outputs=details_output)
59
 
60
  # Launch the Gradio application
61
  demo.launch()
 
3
  import os
4
  import json
5
  import google.generativeai as genai
6
+ from bs4 import BeautifulSoup
7
 
8
  # Load environment variables
9
  genai.configure(api_key=os.environ["geminiapikey"])
 
20
  }
21
  """
22
 
23
+
24
+ def websearch(prompt):
25
+
26
+ url = f"https://www.google.com/search?q={prompt}"
27
+ headers = {
28
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
29
+ }
30
+
31
+ try:
32
+ response = requests.get(url, headers=headers)
33
+ response.raise_for_status() # Wirft eine Exception für Fehlercodes
34
+ except requests.exceptions.RequestException as e:
35
+ print(f"Fehler beim Abrufen der Google-Seite: {e}")
36
+ return None
37
+
38
+ soup = BeautifulSoup(response.content, 'html.parser')
39
+
40
+ first_div = soup.find('div', class_='MjjYud')
41
+
42
+ if first_div:
43
+ return first_div.text.strip()
44
+ else:
45
+ print("Kein div mit der Klasse 'MjjYud' gefunden.")
46
+ return None
47
+
48
  def predict(prompt):
49
  # Create the model
50
  generation_config = {
 
81
  button = gr.Button("Senden")
82
 
83
  # Connect the button to the function
84
+ button.click(fn=websearch, inputs=ort_input, outputs=details_output)
85
 
86
  # Launch the Gradio application
87
  demo.launch()