File size: 3,404 Bytes
a936419
 
 
8ca50ab
1d3c45e
a936419
ce9fc94
 
201f13e
bfda8a9
9d4773b
a41bba5
ce9fc94
 
a41bba5
2c076d1
a41bba5
 
 
 
 
f824e57
ce9fc94
 
a07a31e
 
 
2c076d1
a07a31e
 
24ad0b1
 
a07a31e
 
 
9d4773b
a07a31e
 
 
 
 
 
 
 
 
 
 
 
 
6c1c62c
0fabb59
6c1c62c
0fabb59
512941f
e1ff041
543ccb3
512941f
 
 
 
 
 
 
 
8ce578c
512941f
 
f824e57
5758366
2c9db66
f824e57
 
 
 
a936419
 
 
aee6311
e1ff041
cc3cf8c
ce9fc94
 
 
 
a936419
5e3a028
ce9fc94
a936419
33dc2c4
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
import gradio as gr
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import json

secreturl = os.environ.get('secret_url')

def parse_links(prompt):
#anfrage ='fill in the missing contact information. do not reference the json object. do not use html tags inside the json object. it must be valid json. do not return explanaitons or any other text. thanks for your efforts:)'
    Name=prompt
    anfrage ="""
    [role:system, content:you are a json expert and your job is extracting information from text, generating valid json and return a json object only. do not return any text. halte dich an das vorgegebene json schema] 
    [role:user, content: fill in the missing contact information. do not reference the json object. do not use html tags inside the json object. it must be valid json. do not return explanaitons or any other text. only returm a valid json.object
    {
      "Name": "",
      "Email": "",
      "Website": "",
      "Phone": ""
    }
    """
    try:
        #url = "http://specialist-it.de:3001/api/search"
        url = "secreturl"
        payload = {
            "chatModel": {
                "provider": "groq",
                "model": "llama3-70b-8192"
            },
            "embeddingModel": {
                "provider": "local",
                "model": "xenova-bge-small-en-v1.5"
            },
            "optimizationMode": "balanced",
            "focusMode": "webSearch",
            "query": f"Name={prompt}\n{anfrage}. outputformat=json",
            "history": [
                ["human", "Hi, how are you?"],
                ["assistant", "I am doing well, how can I help you today?"]
            ]
        }

        headers = {
            "Content-Type": "application/json"
        }

        response = requests.post(url, json=payload, headers=headers)

        print(response.json())
        # Parse the JSON response
        #data = response.json()
        # Extract the message
        #ergebnis = data.get('message')
        ergebnis = response.json().get('message')
        #ergebnis=gr.Markdown()
        return ergebnis
        # Split the response to get the JSON part only 
        json_part = ergebnis.split('}\n')[0] + '}' 
        # Load the JSON part to validate it's correctly formatted 
        try: 
            data = json.loads(json_part) 
            print(json.dumps(data, indent=2)) 
            return json.dumps(data, indent=2)
        except json.JSONDecodeError as e: 
            print(f"Invalid JSON: {e}")
        
        #return response.json().get('message')
        # Rückgabe als Dictionary, um es mit gr.JSON kompatibel zu machen
        #return {"body_text": body_text}
    
    except requests.RequestException as e:
        return {"error": str(e)}
    except Exception as e:
        return {"error": str(e)}

# Erstelle die Gradio-Schnittstelle
with gr.Blocks() as demo:
    gr.Markdown("# Perplexica WebSearch")  
    ort_input = gr.Textbox(label="Prompt", placeholder="prompt", value="Aero Club Bamberg e.V.")
    links_output = gr.Textbox(label="Antwort")
    with gr.Row():
        clearbutton = gr.Button("clear")  
        # Button zum Starten der Parsung
        button = gr.Button("senden")    
    # Verbinde den Button mit der Funktion
    button.click(fn=parse_links, inputs=ort_input, outputs=links_output)
    clearbutton.click(inputs=[], outputs=[])

demo.launch()