Spaces:
Sleeping
Sleeping
Saif Rehman Nasir
commited on
Commit
•
0f78fcf
1
Parent(s):
a8ae450
Add RAG related data
Browse files- app.py +32 -1
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,11 +1,25 @@
|
|
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,
|
@@ -23,6 +37,23 @@ def respond(
|
|
23 |
if val[1]:
|
24 |
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
response = ""
|
@@ -45,7 +76,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
|
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a
|
49 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
gr.Slider(
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
+
|
5 |
+
from pinecone import Pinecone, ServerlessSpec
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
|
13 |
"""
|
14 |
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
|
15 |
"""
|
16 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
17 |
|
18 |
+
embedding_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
19 |
+
|
20 |
+
pinecone_client = Pinecone(api_key = os.getenv('PINECONE_API_KEY'))
|
21 |
+
|
22 |
+
index = pinecone_client.Index("movies")
|
23 |
|
24 |
def respond(
|
25 |
message,
|
|
|
37 |
if val[1]:
|
38 |
messages.append({"role": "assistant", "content": val[1]})
|
39 |
|
40 |
+
# encode user query
|
41 |
+
encoded_query = embedding_model.encode(message)
|
42 |
+
|
43 |
+
# retrieve most relevant movie from vector db
|
44 |
+
matches = index.query(
|
45 |
+
vector= encoded_query.tolist(),
|
46 |
+
top_k=1,
|
47 |
+
include_metadata = True
|
48 |
+
)
|
49 |
+
|
50 |
+
# movie which is most similar
|
51 |
+
retrieved_data = matches['matches'][0]['metadata']['title']
|
52 |
+
|
53 |
+
# Add as context to LLM
|
54 |
+
messages.append({"role":"user", "content": retrieved_data})
|
55 |
+
|
56 |
+
|
57 |
messages.append({"role": "user", "content": message})
|
58 |
|
59 |
response = ""
|
|
|
76 |
demo = gr.ChatInterface(
|
77 |
respond,
|
78 |
additional_inputs=[
|
79 |
+
gr.Textbox(value="You are a movie recommender named Exodia. You are extremely reliable. You always mention your name in the beginning of conversation. You will provide me with answers from the given info.", label="System message"),
|
80 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
81 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
82 |
gr.Slider(
|
requirements.txt
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
huggingface_hub==0.22.2
|
|
|
|
|
|
1 |
+
huggingface_hub==0.22.2
|
2 |
+
pinecone-client==5.0.0
|
3 |
+
sentence_transformers
|