Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,76 @@
|
|
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
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 |
-
|
27 |
-
|
|
|
28 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
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 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from langchain.vectorstores import FAISS
|
4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain.text_splitter import CharacterTextSplitter
|
6 |
+
from langchain.document_loaders import TextLoader
|
7 |
|
|
|
|
|
|
|
8 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
9 |
|
10 |
+
# Funktion zum Laden und Indexieren eines Dokuments
|
11 |
+
def load_and_index_document(file_path: str):
|
12 |
+
loader = TextLoader(file_path)
|
13 |
+
documents = loader.load()
|
14 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
15 |
+
chunks = text_splitter.split_documents(documents)
|
16 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
17 |
+
vector_store = FAISS.from_documents(chunks, embeddings)
|
18 |
+
return vector_store
|
19 |
|
20 |
+
# Antwortfunktion für den RAG-Chatbot
|
21 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, file):
|
22 |
+
# Dateipfad des hochgeladenen Dokuments
|
23 |
+
file_path = file.name
|
24 |
+
|
25 |
+
# Dokument laden und indexieren
|
26 |
+
vector_store = load_and_index_document(file_path)
|
27 |
+
|
28 |
+
# Historie und Systemnachricht aufbereiten
|
29 |
messages = [{"role": "system", "content": system_message}]
|
|
|
30 |
for val in history:
|
31 |
if val[0]:
|
32 |
messages.append({"role": "user", "content": val[0]})
|
33 |
if val[1]:
|
34 |
messages.append({"role": "assistant", "content": val[1]})
|
35 |
+
|
36 |
+
# Abruf relevanter Abschnitte aus dem Dokument
|
37 |
+
docs = vector_store.similarity_search(message, k=3) # Abrufen von 3 relevanten Dokumentabschnitten
|
38 |
+
context = "\n".join([doc.page_content for doc in docs])
|
39 |
|
40 |
+
# Nachricht an das Modell
|
41 |
+
full_message = f"{context}\n\nUser: {message}\nAssistant:"
|
42 |
+
|
43 |
response = ""
|
44 |
+
try:
|
45 |
+
# Generierung der Antwort
|
46 |
+
for message in client.chat_completion(
|
47 |
+
[{"role": "system", "content": system_message}, {"role": "user", "content": full_message}],
|
48 |
+
max_tokens=max_tokens,
|
49 |
+
stream=True,
|
50 |
+
temperature=temperature,
|
51 |
+
top_p=top_p,
|
52 |
+
):
|
53 |
+
token = message.choices[0].delta.content
|
54 |
+
response += token
|
55 |
+
yield response
|
56 |
+
except Exception as e:
|
57 |
+
yield f"An error occurred: {str(e)}"
|
58 |
|
59 |
+
# Gradio-UI erstellen
|
60 |
+
def create_gradio_ui():
|
61 |
+
demo = gr.Interface(
|
62 |
+
fn=respond,
|
63 |
+
inputs=[
|
64 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
65 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
66 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
67 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
68 |
+
gr.File(label="Upload Document") # Datei-Upload
|
69 |
+
],
|
70 |
+
live=True
|
71 |
+
)
|
72 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
if __name__ == "__main__":
|
75 |
+
ui = create_gradio_ui()
|
76 |
+
ui.launch()
|