File size: 4,204 Bytes
c3b7bd1
 
 
f542379
df9dd4d
17614fa
 
 
013dcaf
1166e1a
1aa8a78
 
89234f1
2d0e802
17614fa
 
 
1166e1a
 
1aa8a78
 
 
 
2d0e802
1aa8a78
 
 
 
7a04bdb
1aa8a78
 
1c3476f
1aa8a78
 
 
 
 
 
89c900b
a0f1a0e
b645e18
1739a6e
 
c5414cf
2d0e802
7f3deda
 
 
1739a6e
d1b76e1
1a882eb
 
9d5b7e0
1a882eb
 
d1b76e1
1a882eb
 
 
 
 
 
7ff6b2b
5130187
 
d16d2ac
 
 
 
 
6d31c60
adc995e
affdefa
f0dbbfe
5130187
f0dbbfe
0afdb98
63b9b65
c5414cf
6e97436
 
 
 
 
c5414cf
 
59fd07b
 
 
 
 
6d31c60
59fd07b
 
6d31c60
1c3476f
59fd07b
b8db0a7
59fd07b
 
351fd1d
0d04d2e
3f8a236
6ca8163
92796cf
 
c3b7bd1
 
2f7e2da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
#from selenium.webdriver.firefox.options import Options 
import groq
import os
import time
import requests

options = webdriver.ChromeOptions()
options.add_argument('--headless')
wd = webdriver.Chrome(options=options)
#wd = webdriver.Firefox(options=options)
#get your api-key @groq.com. its free!
api_key = os.getenv('groq')
client = groq.Client(api_key=api_key)


# Use Llama 3 70B powered by Groq for answering
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 = "https://chat.deepseek.com/"
    #url = 'https://www.spiegel.de'
    #url = f"https://duckduckgo.com/?q=impressum {message}" 
    url = f"https://search.brave.com/search?q=impressum {message}" 
    #<ol class="AmbQnf">

    wd.get(url)
    time.sleep(3)
    element = wd.find_element(By.TAG_NAME, "body")
    #time.sleep(3)
    #return element.text     
    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)}"
   

    #text_input = wd.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div[1]/textarea')
    
    wait = WebDriverWait(wd, 25)  # Korrektur der Instanziierung
    element = wait.until(EC.visibility_of_element_located((By.ID, "chat-input")))  # Korrektur der Verwendung von EC 
    #element = WebDriverWait(wd, 25).until(
        #EC.presence_of_element_located((By.ID, 'chat-input'))
    #)
    time.sleep(5)
    #textarea = wd.find_element(By.XPATH, "/html/body/div/main/div[3]/div[2]/div/div[2]/div/div/div[3]/div[2]/div[1]/div/div/div/div/textarea")   
    #textarea = wd.find_element(By.XPATH, "/html/body/div[1]/div[2]/main/div[1]/div[2]/div/div[1]/div/form/div/div[2]/div/div/div[1]/div[1]/div/textarea") 
    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 element.text
    return out

    
    texts=""
    url = f"https://www.google.com/search?q=zugverbindung+bad+kissingen+{message}"
    #url = 'https://www.spiegel.de'
    #<ol class="AmbQnf">
    wd.get(url)
    wd.implicitly_wait(5)
    element = wd.find_element(By.TAG_NAME, "body")              
    #wd.quit()
    time.sleep(5)
    #return element.text
    results = update(element.text, message)
    results=gr.Markdown()
    return results
          
iface = gr.Interface(
    fn=selenium,
    inputs="text",
    outputs="text",
    #title="perplexity.ai",
    #description="Websuche"
)

iface.launch()