|
import gradio as gr |
|
from selenium import webdriver |
|
from selenium.common.exceptions import WebDriverException |
|
from selenium.webdriver.common.by import By |
|
from gradio_client import Client |
|
from selenium.webdriver.support.wait import WebDriverWait |
|
from selenium.webdriver.support import expected_conditions as EC |
|
from selenium.webdriver.chrome.options import Options |
|
from selenium.webdriver.common.keys import Keys |
|
|
|
import groq |
|
import os |
|
import time |
|
import requests |
|
|
|
options = webdriver.ChromeOptions() |
|
options.add_argument('--headless') |
|
wd = webdriver.Chrome(options=options) |
|
|
|
|
|
api_key = os.getenv('groq') |
|
client = groq.Client(api_key=api_key) |
|
|
|
|
|
|
|
def update(prompt, ort): |
|
try: |
|
completion = client.chat.completions.create( |
|
model="llama3-70b-8192", |
|
messages=[ |
|
{"role": "system", "content": "You are a helpful assistant."}, |
|
{"role": "user", "content": f"gefragt sind die nächsten 3 zugverbindungen von bad kissingen nach {ort} du findest die antwort im kontext. liefere als antwort ein 2 spaltige tabelle. linke spalte: abfahrtszeit, fahrtdauer, ankunftszeit.rechte spalte: abfahrtsort,leer,zielort. formatiere die tabelle in markdown\n kontext: \n {prompt} \n antworte immer auf deutsch!"} |
|
], |
|
) |
|
return completion.choices[0].message.content |
|
except Exception as e: |
|
return f"Error in response generation: {str(e)}" |
|
|
|
def selenium(message): |
|
|
|
|
|
|
|
url = f"https://search.brave.com/search?q=impressum {message}" |
|
|
|
|
|
wd.get(url) |
|
time.sleep(3) |
|
element = wd.find_element(By.TAG_NAME, "body") |
|
|
|
|
|
try: |
|
completion = client.chat.completions.create( |
|
model="llama3-8b-8192", |
|
messages=[ |
|
{"role": "system", "content": "You are a helpful assistant."}, |
|
{"role": "user", "content": f"return json object with keys name and email. \n name = {message}\n value for email can be found here: {element.text} \n return json object only, no additional text or comments \n"} |
|
], |
|
) |
|
return completion.choices[0].message.content |
|
except Exception as e: |
|
return f"Error in response generation: {str(e)}" |
|
|
|
|
|
|
|
|
|
wait = WebDriverWait(wd, 25) |
|
element = wait.until(EC.visibility_of_element_located((By.ID, "chat-input"))) |
|
|
|
|
|
|
|
time.sleep(5) |
|
|
|
|
|
element.send_keys(f"{message}") |
|
|
|
element.send_keys(Keys.RETURN) |
|
time.sleep(8) |
|
elements = wd.find_elements(By.TAG_NAME, "p") |
|
time.sleep(3) |
|
out="" |
|
for e in elements: |
|
out +=e.text |
|
|
|
return out |
|
|
|
|
|
texts="" |
|
url = f"https://www.google.com/search?q=zugverbindung+bad+kissingen+{message}" |
|
|
|
|
|
wd.get(url) |
|
wd.implicitly_wait(5) |
|
element = wd.find_element(By.TAG_NAME, "body") |
|
|
|
time.sleep(5) |
|
|
|
results = update(element.text, message) |
|
results=gr.Markdown() |
|
return results |
|
|
|
iface = gr.Interface( |
|
fn=selenium, |
|
inputs="text", |
|
outputs="text", |
|
|
|
|
|
) |
|
|
|
iface.launch() |
|
|