PerplexicaApi / app.py
mgokg's picture
Update app.py
b22d092 verified
raw
history blame
4.17 kB
import gradio as gr
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import json
import os
secreturl = os.environ.get('secret_url')
custom_css = """
#md {
height: 350px;
font-size:20px;
background:grey;
padding: 20px;
color:black:
}
#senden {
width: 200px; float:right;
}
#clear {
width: 200px; float:right;
}
"""
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
#outputs.value = "# Bitte warten"
#suite your prompt to your needs
anfrage ="""
instruction: 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.
prompt: fill in the missing contact information. do not reference the json object. do not use html tags inside the json object. do not return explanaitons or any other text. return a valid json object only.
{
"Name": "",
"Email": "",
"Website": "",
"Phone": ""
}
"""
try:
#get Perplexica on github
#http:// url to perplexica api running inside a docker container. you may use certbot to issue a certificate for the perplexica webserver
url = secreturl
payload = {
"chatModel": {
"provider": "groq",
"model": "llama3-70b-8192"
},
"embeddingModel": {
"provider": "local",
"model": "xenova-bge-small-en-v1.5"
},
"optimizationMode": "speed",
"focusMode": "webSearch",
#"query": f"Impressum Name={prompt}\n{anfrage}. outputformat=json",
"query": f"antworte kurz und knapp. {prompt}. antworte auf deutsch. return plain text only für text zu sprache\n",
"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)
ergebnis = response.json().get('message')
#ergebnis=gr.Markdown()
ergebnis =ergebnis.replace("[1]", " ")
ergebnis =ergebnis.replace("[2]", " ")
ergebnis =ergebnis.replace("[3]", " ")
ergebnis =ergebnis.replace("[4]", " ")
ergebnis =ergebnis.replace("[5]", " ")
ergebnis =ergebnis.replace("[6]", " ")
ergebnis =ergebnis.replace("[7]", " ")
ergebnis =ergebnis.replace("[8]", " ")
ergebnis =ergebnis.replace("[9]", " ")
ergebnis =ergebnis.replace("[10]", " ")
ergebnis =ergebnis.replace("[11]", " ")
ergebnis =ergebnis.replace("[12]", " ")
ergebnis =ergebnis.replace("[13]", " ")
ergebnis =ergebnis.replace("[14]", " ")
ergebnis =ergebnis.replace("[15]", " ")
ergebnis =ergebnis.replace("[16]", " ")
ergebnis =ergebnis.replace("[17]", " ")
ergebnis =ergebnis.replace("[18]", " ")
return ergebnis
# return as Dictionary, for gr.JSON
#return {"body_text": body_text}
except requests.RequestException as e:
return {"error": str(e)}
except Exception as e:
return {"error": str(e)}
#clear output
def clear():
clear=""
return clear
# Erstelle die Gradio-Schnittstelle
with gr.Blocks(css=custom_css, theme = gr.themes.Soft()) as demo:
gr.Markdown("# Perplexica WebSearch ")
links_output = gr.Markdown(label="Antwort",elem_id="md")
ort_input = gr.Textbox(label="Frage", placeholder="ask anything...", value="")
#clearbutton = gr.Button("clear", elem_id="clear")
button = gr.Button("senden", elem_id="senden")
button.click(fn=parse_links, inputs=ort_input, outputs=links_output)
#clearbutton.click(fn=clear, inputs=[], outputs=links_output)
demo.launch()