initial code
Browse files- app.py +81 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import weaviate.classes as wvc
|
4 |
+
import weaviate
|
5 |
+
from weaviate.auth import AuthApiKey
|
6 |
+
import logging
|
7 |
+
import os
|
8 |
+
import requests
|
9 |
+
import json
|
10 |
+
|
11 |
+
"""
|
12 |
+
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
|
13 |
+
"""
|
14 |
+
|
15 |
+
logging.basicConfig(level=logging.INFO)
|
16 |
+
|
17 |
+
client = weaviate.connect_to_embedded(
|
18 |
+
headers={
|
19 |
+
"X-Huggingface-Api-Key": os.environ["HF_TOKEN"] # Replace with your inference API key
|
20 |
+
}
|
21 |
+
)
|
22 |
+
|
23 |
+
if client.is_ready():
|
24 |
+
logging.info('')
|
25 |
+
logging.info(f'Found {len(client.cluster.nodes())} Weaviate nodes.')
|
26 |
+
logging.info('')
|
27 |
+
for node in client.cluster.nodes():
|
28 |
+
logging.info(node)
|
29 |
+
logging.info('')
|
30 |
+
|
31 |
+
client.collections.delete_all()
|
32 |
+
|
33 |
+
questions = client.collections.create(
|
34 |
+
name="Question",
|
35 |
+
vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_huggingface(wait_for_model=True),
|
36 |
+
generative_config=wvc.config.Configure.Generative.openai()
|
37 |
+
)
|
38 |
+
resp = requests.get('https://raw.githubusercontent.com/databyjp/wv_demo_uploader/main/weaviate_datasets/data/jeopardy_1k.json')
|
39 |
+
data = json.loads(resp.text) # Load data
|
40 |
+
|
41 |
+
question_objs = list()
|
42 |
+
for i, d in enumerate(data):
|
43 |
+
question_objs.append({
|
44 |
+
"answer": d["Answer"],
|
45 |
+
"question": d["Question"],
|
46 |
+
"category": d["Category"],
|
47 |
+
"air_date": d["Air Date"],
|
48 |
+
"round": d["Round"],
|
49 |
+
"value": d["Value"]
|
50 |
+
})
|
51 |
+
|
52 |
+
logging.info('Importing Questions')
|
53 |
+
questions = client.collections.get("Question")
|
54 |
+
questions.data.insert_many(question_objs)
|
55 |
+
logging.info('Finished Importing Questions')
|
56 |
+
|
57 |
+
def respond(query):
|
58 |
+
|
59 |
+
r = ""
|
60 |
+
if client.is_ready():
|
61 |
+
r = f'Found {len(client.cluster.nodes())} Weaviate nodes.'
|
62 |
+
|
63 |
+
response = questions.query.near_text(
|
64 |
+
query=query,
|
65 |
+
limit=2
|
66 |
+
)
|
67 |
+
|
68 |
+
return response.objects[0].properties
|
69 |
+
|
70 |
+
demo = gr.Interface(fn=respond,
|
71 |
+
inputs=gr.Textbox(
|
72 |
+
label="Search the Jeopardy Vector Database",
|
73 |
+
info="Query:",
|
74 |
+
lines=1,
|
75 |
+
value="Guitar",
|
76 |
+
),
|
77 |
+
outputs="textbox"
|
78 |
+
)
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.22.2
|
2 |
+
weaviate_client
|
3 |
+
gradio
|