Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
from langchain.vectorstores import Vectara
|
5 |
+
from sentence_transformers import CrossEncoder
|
6 |
+
|
7 |
+
# Input your API keys
|
8 |
+
vectara_customer_id = "3939498282"
|
9 |
+
vectara_corpus_id = 2
|
10 |
+
vectara_api_key = 'zqt_6s_5KqwCCxK5tosYGbpSie8n2-hO7LdlxBWUBA'
|
11 |
+
|
12 |
+
# Initialize Vectara
|
13 |
+
vectara_instance = Vectara(
|
14 |
+
vectara_customer_id='3939498282',
|
15 |
+
vectara_corpus_id=2,
|
16 |
+
vectara_api_key='zqt_Y3kD9bueJq3QO5t_FISVQLmgTWMDhzgMgK9Isw',
|
17 |
+
)
|
18 |
+
|
19 |
+
# Model initialization
|
20 |
+
model = CrossEncoder('vectara/hallucination_evaluation_model')
|
21 |
+
|
22 |
+
# Streamlit app
|
23 |
+
st.title('RAG-Based App')
|
24 |
+
|
25 |
+
# Input message from the user
|
26 |
+
message = st.text_input('Enter your message')
|
27 |
+
|
28 |
+
# Button to trigger the processing
|
29 |
+
if st.button('Process'):
|
30 |
+
# Processing logic
|
31 |
+
corpus_key = [
|
32 |
+
{
|
33 |
+
"customerId": vectara_customer_id,
|
34 |
+
"corpusId": vectara_corpus_id,
|
35 |
+
"lexicalInterpolationConfig": {"lambda": 0.025},
|
36 |
+
}
|
37 |
+
]
|
38 |
+
data = {
|
39 |
+
"query": [
|
40 |
+
{
|
41 |
+
"query": message,
|
42 |
+
"start": 0,
|
43 |
+
"numResults": 10,
|
44 |
+
"contextConfig": {
|
45 |
+
"sentencesBefore": 2,
|
46 |
+
"sentencesAfter": 2,
|
47 |
+
},
|
48 |
+
"corpusKey": corpus_key,
|
49 |
+
"summary": [
|
50 |
+
{
|
51 |
+
"responseLang": "eng",
|
52 |
+
"maxSummarizedResults": 5,
|
53 |
+
}
|
54 |
+
]
|
55 |
+
}
|
56 |
+
]
|
57 |
+
}
|
58 |
+
|
59 |
+
headers = {
|
60 |
+
"x-api-key": vectara_api_key,
|
61 |
+
"customer-id": vectara_customer_id,
|
62 |
+
"Content-Type": "application/json",
|
63 |
+
}
|
64 |
+
response = requests.post(
|
65 |
+
headers=headers,
|
66 |
+
url="https://api.vectara.io/v1/query",
|
67 |
+
data=json.dumps(data),
|
68 |
+
)
|
69 |
+
if response.status_code != 200:
|
70 |
+
st.error("Query failed")
|
71 |
+
else:
|
72 |
+
result = response.json()
|
73 |
+
responses = result["responseSet"][0]["response"]
|
74 |
+
summary = result["responseSet"][0]["summary"][0]["text"]
|
75 |
+
|
76 |
+
res = [[r['text'], r['score']] for r in responses]
|
77 |
+
texts = [r[0] for r in res[:5]]
|
78 |
+
scores = [model.predict([text, summary]) for text in texts]
|
79 |
+
|
80 |
+
text_elements = []
|
81 |
+
docs = vectara_instance.similarity_search(message)
|
82 |
+
for source_idx, source_doc in enumerate(docs[:5]):
|
83 |
+
source_name = f"Source {source_idx + 1}"
|
84 |
+
text_elements.append(source_doc.page_content)
|
85 |
+
|
86 |
+
ans = f"{summary}\n HHEM Scores: {scores}"
|
87 |
+
st.text(ans)
|
88 |
+
st.text("Sources:")
|
89 |
+
for text in text_elements:
|
90 |
+
st.text(text)
|