Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,20 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -15,19 +24,25 @@ def respond(
|
|
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 |
-
|
|
|
|
|
|
|
27 |
|
28 |
response = ""
|
29 |
|
30 |
-
|
|
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
33 |
stream=True,
|
@@ -35,14 +50,10 @@ def respond(
|
|
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=[
|
@@ -59,6 +70,5 @@ demo = gr.ChatInterface(
|
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import chromadb
|
4 |
+
from chromadb.config import Settings
|
5 |
|
6 |
+
# Initialize ChromaDB client
|
7 |
+
client_db = chromadb.Client(Settings(chroma_db_impl="duckdb+parquet", persist_directory="path/to/your/chromadb"))
|
|
|
|
|
8 |
|
9 |
+
# Load your collection
|
10 |
+
collection = client_db.get_collection("your_collection_name")
|
11 |
+
|
12 |
+
# Initialize the Hugging Face Inference Client
|
13 |
+
inference_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
14 |
+
|
15 |
+
def retrieve_from_chromadb(query):
|
16 |
+
results = collection.query(query=query, n_results=5) # Adjust n_results as needed
|
17 |
+
return results['documents']
|
18 |
|
19 |
def respond(
|
20 |
message,
|
|
|
24 |
temperature,
|
25 |
top_p,
|
26 |
):
|
27 |
+
# Prepare messages for the model
|
28 |
messages = [{"role": "system", "content": system_message}]
|
29 |
|
30 |
+
# Add conversation history
|
31 |
for val in history:
|
32 |
if val[0]:
|
33 |
messages.append({"role": "user", "content": val[0]})
|
34 |
if val[1]:
|
35 |
messages.append({"role": "assistant", "content": val[1]})
|
36 |
|
37 |
+
# Retrieve relevant documents from ChromaDB
|
38 |
+
retrieved_docs = retrieve_from_chromadb(message)
|
39 |
+
context = "\n".join(retrieved_docs) + "\nUser: " + message
|
40 |
+
messages.append({"role": "user", "content": context})
|
41 |
|
42 |
response = ""
|
43 |
|
44 |
+
# Generate response using the Inference Client
|
45 |
+
for message in inference_client.chat_completion(
|
46 |
messages,
|
47 |
max_tokens=max_tokens,
|
48 |
stream=True,
|
|
|
50 |
top_p=top_p,
|
51 |
):
|
52 |
token = message.choices[0].delta.content
|
|
|
53 |
response += token
|
54 |
yield response
|
55 |
|
56 |
+
# Gradio Chat Interface
|
|
|
|
|
|
|
57 |
demo = gr.ChatInterface(
|
58 |
respond,
|
59 |
additional_inputs=[
|
|
|
70 |
],
|
71 |
)
|
72 |
|
|
|
73 |
if __name__ == "__main__":
|
74 |
+
demo.launch()
|