Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,88 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
client = InferenceClient("
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import urllib.request
|
| 4 |
+
import xml.etree.ElementTree as ET
|
| 5 |
+
|
| 6 |
+
# HuggingFace Inference Client
|
| 7 |
+
client = InferenceClient("meta-llama/Llama-3.3-70B-Instruct")
|
| 8 |
+
|
| 9 |
+
# Funktion, um die Eingabe zu bereinigen und einen prägnanten Query zu erstellen
|
| 10 |
+
def generate_query(input_text):
|
| 11 |
+
stopwords = ["welche", "gibt", "es", "zum", "thema", "studien", "über", "zu", "dem"]
|
| 12 |
+
words = input_text.lower().split()
|
| 13 |
+
query = " ".join([word for word in words if word not in stopwords])
|
| 14 |
+
return query.strip()
|
| 15 |
+
|
| 16 |
+
# Funktion, um relevante Studien von arXiv zu suchen
|
| 17 |
+
def fetch_arxiv_summary(query, sort_by="relevance", sort_order="descending", max_results=20):
|
| 18 |
+
url = (f'http://export.arxiv.org/api/query?search_query=all:{urllib.parse.quote(query)}'
|
| 19 |
+
f'&start=0&max_results={max_results}&sortBy={sort_by}&sortOrder={sort_order}')
|
| 20 |
+
try:
|
| 21 |
+
data = urllib.request.urlopen(url)
|
| 22 |
+
xml_data = data.read().decode("utf-8")
|
| 23 |
+
root = ET.fromstring(xml_data)
|
| 24 |
+
summaries = []
|
| 25 |
+
for entry in root.findall(".//{http://www.w3.org/2005/Atom}entry"):
|
| 26 |
+
summary = entry.find("{http://www.w3.org/2005/Atom}summary")
|
| 27 |
+
if summary is not None:
|
| 28 |
+
summaries.append(summary.text.strip())
|
| 29 |
+
return summaries if summaries else ["Keine relevanten Studien gefunden."]
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return [f"Fehler beim Abrufen der Studie: {str(e)}"]
|
| 32 |
+
|
| 33 |
+
# Chatbot-Logik mit arXiv-Integration
|
| 34 |
+
def respond(
|
| 35 |
+
message,
|
| 36 |
+
history: list[tuple[str, str]],
|
| 37 |
+
system_message,
|
| 38 |
+
max_tokens,
|
| 39 |
+
temperature,
|
| 40 |
+
top_p,
|
| 41 |
+
sort_by,
|
| 42 |
+
sort_order,
|
| 43 |
+
max_results,
|
| 44 |
+
):
|
| 45 |
+
# Query generieren und Studien abrufen
|
| 46 |
+
query = generate_query(message)
|
| 47 |
+
study_summaries = fetch_arxiv_summary(query, sort_by, sort_order, max_results)
|
| 48 |
+
study_info = "\n".join(study_summaries)
|
| 49 |
+
|
| 50 |
+
# Nachrichten vorbereiten
|
| 51 |
+
messages = [{"role": "system", "content": system_message}]
|
| 52 |
+
for val in history:
|
| 53 |
+
if val[0]:
|
| 54 |
+
messages.append({"role": "user", "content": val[0]})
|
| 55 |
+
if val[1]:
|
| 56 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 57 |
+
|
| 58 |
+
messages.append({"role": "user", "content": f"{message}\nStudien-Info:\n{study_info}"})
|
| 59 |
+
|
| 60 |
+
# Antwort vom Modell generieren
|
| 61 |
+
response = ""
|
| 62 |
+
for message in client.chat_completion(
|
| 63 |
+
messages,
|
| 64 |
+
max_tokens=max_tokens,
|
| 65 |
+
stream=True,
|
| 66 |
+
temperature=temperature,
|
| 67 |
+
top_p=top_p,
|
| 68 |
+
):
|
| 69 |
+
token = message.choices[0].delta.content
|
| 70 |
+
response += token
|
| 71 |
+
yield response
|
| 72 |
+
|
| 73 |
+
# Gradio-Interface mit zusätzlichen Eingaben
|
| 74 |
+
demo = gr.ChatInterface(
|
| 75 |
+
respond,
|
| 76 |
+
additional_inputs=[
|
| 77 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 78 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 79 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 80 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 81 |
+
gr.Dropdown(label="Sortieren nach", choices=["relevance", "lastUpdatedDate", "submittedDate"], value="relevance"),
|
| 82 |
+
gr.Dropdown(label="Sortierreihenfolge", choices=["ascending", "descending"], value="descending"),
|
| 83 |
+
gr.Slider(label="Maximale Ergebnisse", minimum=1, maximum=50, value=20, step=1),
|
| 84 |
+
],
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch()
|