Sebbe33 commited on
Commit
1e803da
·
verified ·
1 Parent(s): de36147

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -64
app.py CHANGED
@@ -1,64 +1,88 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()