Spaces:
Runtime error
Runtime error
eaglelandsonce
commited on
Commit
•
1871536
1
Parent(s):
f5ce1a8
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,86 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
import os
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
import os
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
# Assuming the environment variables are already set, we directly use them.
|
8 |
+
# However, in a Streamlit app, you might want to set them up within the script for demonstration purposes
|
9 |
+
# or securely use secrets management for API keys and other sensitive information.
|
10 |
+
|
11 |
+
# Define the Vectara query function
|
12 |
+
def vectara_query(query: str, config: dict):
|
13 |
+
"""Query Vectara and return the results."""
|
14 |
+
corpus_key = [{
|
15 |
+
"customerId": config["customer_id"],
|
16 |
+
"corpusId": config["corpus_id"],
|
17 |
+
"lexicalInterpolationConfig": {"lambda": config.get("lambda_val", 0.5)},
|
18 |
+
}]
|
19 |
+
data = {
|
20 |
+
"query": [{
|
21 |
+
"query": query,
|
22 |
+
"start": 0,
|
23 |
+
"numResults": config.get("top_k", 10),
|
24 |
+
"contextConfig": {
|
25 |
+
"sentencesBefore": 2,
|
26 |
+
"sentencesAfter": 2,
|
27 |
+
},
|
28 |
+
"corpusKey": corpus_key,
|
29 |
+
"summary": [{
|
30 |
+
"responseLang": "eng",
|
31 |
+
"maxSummarizedResults": 5,
|
32 |
+
}]
|
33 |
+
}]
|
34 |
+
}
|
35 |
+
|
36 |
+
headers = {
|
37 |
+
"x-api-key": config["api_key"],
|
38 |
+
"customer-id": config["customer_id"],
|
39 |
+
"Content-Type": "application/json",
|
40 |
+
}
|
41 |
+
response = requests.post(
|
42 |
+
headers=headers,
|
43 |
+
url="https://api.vectara.io/v1/query",
|
44 |
+
data=json.dumps(data),
|
45 |
+
)
|
46 |
+
if response.status_code != 200:
|
47 |
+
st.error(f"Query failed (code {response.status_code}, reason {response.reason}, details {response.text})")
|
48 |
+
return [], ""
|
49 |
+
|
50 |
+
result = response.json()
|
51 |
+
responses = result["responseSet"][0]["response"]
|
52 |
+
summary = result["responseSet"][0]["summary"][0]["text"]
|
53 |
+
|
54 |
+
res = [[r['text'], r['score']] for r in responses]
|
55 |
+
return res, summary
|
56 |
+
|
57 |
+
# Streamlit interface
|
58 |
+
st.title("Vectara Content Query Interface")
|
59 |
+
|
60 |
+
# User inputs
|
61 |
+
query = st.text_input("Enter your query here", "What does Vectara do?")
|
62 |
+
lambda_val = st.slider("Lambda Value", min_value=0.0, max_value=1.0, value=0.5)
|
63 |
+
top_k = st.number_input("Top K Results", min_value=1, max_value=50, value=10)
|
64 |
+
|
65 |
+
if st.button("Query Vectara"):
|
66 |
+
config = {
|
67 |
+
"api_key": os.environ.get("VECTARA_API_KEY", ""),
|
68 |
+
"customer_id": os.environ.get("VECTARA_CUSTOMER_ID", ""),
|
69 |
+
"corpus_id": os.environ.get("VECTARA_CORPUS_ID", ""),
|
70 |
+
"lambda_val": lambda_val,
|
71 |
+
"top_k": top_k,
|
72 |
+
}
|
73 |
+
|
74 |
+
results, summary = vectara_query(query, config)
|
75 |
+
|
76 |
+
if results:
|
77 |
+
st.subheader("Summary")
|
78 |
+
st.write(summary)
|
79 |
+
|
80 |
+
st.subheader("Top Results")
|
81 |
+
df = pd.DataFrame(results, columns=['Text', 'Score'])
|
82 |
+
st.dataframe(df)
|
83 |
+
else:
|
84 |
+
st.write("No results found.")
|
85 |
+
|
86 |
+
# Note: The integration of the model for HHEM scores is omitted as it requires the specific model details and implementation.
|