Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
os.environ['VECTARA_API_KEY'] = 'zqt_UXrBcnI2UXINZkrv4g1tQPhzj02vfdtqYJIDiA'
|
4 |
+
os.environ['VECTARA_CORPUS_ID'] = '1'
|
5 |
+
os.environ['VECTARA_CUSTOMER_ID']='1366999410'
|
6 |
+
|
7 |
+
import os
|
8 |
+
import json
|
9 |
+
import requests
|
10 |
+
import streamlit as st
|
11 |
+
|
12 |
+
def vectara_query(query: str, config: dict) -> None:
|
13 |
+
|
14 |
+
|
15 |
+
corpus_key = [
|
16 |
+
{
|
17 |
+
"customerId": config["customer_id"],
|
18 |
+
"corpusId": config["corpus_id"],
|
19 |
+
"lexicalInterpolationConfig": {"lambda": config["lambda_val"]},
|
20 |
+
}
|
21 |
+
]
|
22 |
+
data = {
|
23 |
+
"query": [
|
24 |
+
{
|
25 |
+
"query": query,
|
26 |
+
"start": 0,
|
27 |
+
"numResults": config["top_k"],
|
28 |
+
"contextConfig": {
|
29 |
+
"sentencesBefore": 2,
|
30 |
+
"sentencesAfter": 2,
|
31 |
+
},
|
32 |
+
"corpusKey": corpus_key,
|
33 |
+
"summary": [
|
34 |
+
{
|
35 |
+
"responseLang": "eng",
|
36 |
+
"maxSummarizedResults": 5,
|
37 |
+
}
|
38 |
+
]
|
39 |
+
}
|
40 |
+
]
|
41 |
+
}
|
42 |
+
|
43 |
+
headers = {
|
44 |
+
"x-api-key": config["api_key"],
|
45 |
+
"customer-id": config["customer_id"],
|
46 |
+
"Content-Type": "application/json",
|
47 |
+
}
|
48 |
+
response = requests.post(
|
49 |
+
headers=headers,
|
50 |
+
url="https://api.vectara.io/v1/query",
|
51 |
+
data=json.dumps(data),
|
52 |
+
)
|
53 |
+
if response.status_code != 200:
|
54 |
+
print(
|
55 |
+
"Query failed %s",
|
56 |
+
f"(code {response.status_code}, reason {response.reason}, details "
|
57 |
+
f"{response.text})",
|
58 |
+
)
|
59 |
+
return []
|
60 |
+
|
61 |
+
result = response.json()
|
62 |
+
responses = result["responseSet"][0]["response"]
|
63 |
+
documents = result["responseSet"][0]["document"]
|
64 |
+
summary = result["responseSet"][0]["summary"][0]["text"]
|
65 |
+
|
66 |
+
res = [[r['text'], r['score']] for r in responses]
|
67 |
+
return res, summary
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
# Set the environment variables
|
73 |
+
os.environ['VECTARA_API_KEY'] = 'zqt_UXrBcnI2UXINZkrv4g1tQPhzj02vfdtqYJIDiA'
|
74 |
+
os.environ['VECTARA_CORPUS_ID'] = '1'
|
75 |
+
os.environ['VECTARA_CUSTOMER_ID'] = '1366999410'
|
76 |
+
|
77 |
+
# Load config from environment variables
|
78 |
+
api_key = os.environ.get("VECTARA_API_KEY", "")
|
79 |
+
customer_id = os.environ.get("VECTARA_CUSTOMER_ID", "")
|
80 |
+
corpus_id = os.environ.get("VECTARA_CORPUS_ID", "")
|
81 |
+
|
82 |
+
config = {
|
83 |
+
"api_key": str(api_key),
|
84 |
+
"customer_id": str(customer_id),
|
85 |
+
"corpus_id": str(corpus_id),
|
86 |
+
"lambda_val": 0.025,
|
87 |
+
"top_k": 10,
|
88 |
+
}
|
89 |
+
|
90 |
+
# Streamlit app
|
91 |
+
st.title("KitchenCreators App")
|
92 |
+
|
93 |
+
# Input for the query
|
94 |
+
query = st.text_input("Enter your query:", "What does Kitchen Creators do?")
|
95 |
+
|
96 |
+
# Button to trigger the query
|
97 |
+
if st.button("Run Query"):
|
98 |
+
results, summary = vectara_query(query, config)
|
99 |
+
|
100 |
+
# Display results
|
101 |
+
st.header("Results")
|
102 |
+
st.write(results)
|
103 |
+
|
104 |
+
# Display summary
|
105 |
+
st.header("Summary")
|
106 |
+
st.write(summary)
|