Spaces:
Running
Running
import gradio as gr | |
import requests | |
from bs4 import BeautifulSoup | |
from urllib.parse import urljoin | |
from datetime import datetime | |
import json | |
import os | |
jetzt = datetime.now() | |
zeitstempel_str = jetzt.strftime("%Y-%m-%d") | |
secreturl = os.environ.get('secret_url') | |
custom_css = """ | |
#md { | |
height: 500px; | |
font-size: 30px; | |
background: #121212; | |
padding: 20px; | |
padding-top: 40px; | |
color: white; | |
border: 1 px solid #383838; | |
} | |
.senden { | |
width: 300px; | |
float: right; | |
background:purple; | |
} | |
.clear-button { | |
float: right; | |
#margin-top: 10px; | |
background:purple; | |
} | |
""" | |
def question(prompt, optimization_mode): | |
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: | |
url = secreturl | |
payload = { | |
"chatModel": { | |
"provider": "groq", | |
"model": "llama3-70b-8192" | |
}, | |
"embeddingModel": { | |
"provider": "local", | |
"model": "xenova-bge-small-en-v1.5" | |
}, | |
"optimizationMode": optimization_mode, | |
"focusMode": "webSearch", | |
"query": f"antworte kurz und knapp. {prompt}. antworte auf deutsch. formatiere deine antworten ansprechend und strukturiert in markdown\nhinweis: heute ist der {zeitstempel_str}\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') | |
for i in range(1, 19): | |
ergebnis = ergebnis.replace(f"[{i}]", " ") | |
return ergebnis | |
except requests.RequestException as e: | |
return {"error": str(e)} | |
except Exception as e: | |
return {"error": str(e)} | |
def clear(): | |
return "", "speed" | |
# Erstelle die Gradio-Schnittstelle | |
with gr.Blocks(css=custom_css, theme=gr.themes.Default(font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"])) as demo: | |
#gr.Markdown("# <br>") | |
links_output = gr.Markdown(label="Antwort", elem_id="md", value="# Perplexica WebSearch") | |
ort_input = gr.Textbox(label="Frage", placeholder="ask anything...", scale=3, value="") | |
optimization_mode_input = gr.Dropdown(choices=["speed", "balanced"], label="Optimization Mode", value="speed") | |
button = gr.Button("senden", elem_classes="senden") | |
button.click(fn=question, inputs=[ort_input, optimization_mode_input], outputs=links_output) | |
#clear_button = gr.Button("clear", elem_classes="clear-button") | |
#clear_button.click(fn=clear, inputs=[], outputs=[ort_input, optimization_mode_input]) | |
demo.launch() |