mgokg commited on
Commit
71aa646
·
verified ·
1 Parent(s): 4427552

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -39
app.py CHANGED
@@ -1,45 +1,75 @@
1
  import gradio as gr
2
- from selenium import webdriver
3
- from selenium.common.exceptions import WebDriverException
4
- from selenium.webdriver.common.by import By
5
- from gradio_client import Client
6
- from selenium.webdriver.support.wait import WebDriverWait
7
- from selenium.webdriver.support import expected_conditions as EC
8
- from selenium.webdriver.chrome.options import Options
9
- from selenium.webdriver.common.keys import Keys
10
- #from selenium.webdriver.firefox.options import Options
11
- import groq
12
- import os
13
- import time
14
  import requests
 
15
 
16
- options = webdriver.ChromeOptions()
17
- options.add_argument('--headless')
18
- wd = webdriver.Chrome(options=options)
19
- #wd = webdriver.Firefox(options=options)
20
- #get your api-key @groq.com. its free!
21
- api_key = os.getenv('groq')
22
- client = groq.Client(api_key=api_key)
23
-
24
- def selenium(message):
25
- #url = "https://chat.deepseek.com/"
26
- #url = 'https://www.spiegel.de'
27
- #url = f"https://duckduckgo.com/?q=impressum {message}"
28
- #url = f"https://search.brave.com/search?q=impressum {message}"
29
- #<ol class="AmbQnf">
30
- url = f"https://www.google.com/search?q=zugverbindung+bad+kissingen+{message}"
31
- wd.get(url)
32
- time.sleep(3)
33
- element = wd.find_element(By.TAG_NAME, "body")
34
- #time.sleep(3)
35
- return element.text
36
-
37
-
38
- iface = gr.Interface(
39
- fn=selenium,
40
- inputs="text",
41
- outputs="text",
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  )
44
 
45
- iface.launch()
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
2
  import requests
3
+ from bs4 import BeautifulSoup
4
 
5
+ # --- Core Function (from the previous answer) ---
6
+ def get_search_body_text(destination: str) -> str:
7
+ """
8
+ Performs a Google search for a train connection using requests and BeautifulSoup.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ This function fetches the search results page, parses the HTML, and returns
11
+ all the text content found within the <body> tags.
12
+
13
+ Args:
14
+ destination (str): The destination city for the train connection search.
15
+
16
+ Returns:
17
+ str: The text content of the <body> tag of the search results page,
18
+ or an error message if the request fails or the body is not found.
19
+ """
20
+ if not destination or not destination.strip():
21
+ return "Please enter a destination city."
22
+
23
+ query = f"zugverbindung bad kissingen {destination}"
24
+ url = "https://www.google.com/search"
25
+ params = {'q': query}
26
+
27
+ # Set a User-Agent header to mimic a real browser
28
+ headers = {
29
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
30
+ }
31
+
32
+ try:
33
+ # Make the HTTP GET request
34
+ response = requests.get(url, params=params, headers=headers, timeout=10)
35
+ response.raise_for_status() # Raise an exception for bad status codes
36
+
37
+ # Parse the HTML content
38
+ soup = BeautifulSoup(response.text, 'html.parser')
39
+ body = soup.find('body')
40
+
41
+ if body:
42
+ # Extract all text, separated by spaces, with extra whitespace stripped
43
+ return body.get_text(separator=' ', strip=True)
44
+ else:
45
+ return "Error: Could not find the <body> tag in the response."
46
+
47
+ except requests.exceptions.RequestException as e:
48
+ return f"Error during request: {e}"
49
+
50
+ # --- Gradio Interface Definition ---
51
+
52
+ # Create the Gradio interface by wrapping the function
53
+ demo = gr.Interface(
54
+ fn=get_search_body_text,
55
+ inputs=gr.Textbox(
56
+ label="Destination City",
57
+ placeholder="e.g., Würzburg, Frankfurt am Main, Nürnberg..."
58
+ ),
59
+ outputs=gr.Textbox(
60
+ label="Raw Body Content from Google Search",
61
+ lines=20 # Give the output box a good height
62
+ ),
63
+ title="Train Connection Search",
64
+ description="Enter a destination city to search for train connections from **Bad Kissingen**. The app fetches the Google search results and displays the raw text content of the page body. Note: The output is unformatted text and can be very long.",
65
+ examples=[
66
+ ["Würzburg"],
67
+ ["Frankfurt am Main"],
68
+ ["Nürnberg"]
69
+ ],
70
+ allow_flagging="never"
71
  )
72
 
73
+ # --- Launch the App ---
74
+ if __name__ == "__main__":
75
+ demo.launch()