wop commited on
Commit
c310176
·
verified ·
1 Parent(s): a0df0a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -1,17 +1,25 @@
1
  import streamlit as st
2
- import json
3
  from gradio_client import Client
4
  import time
 
5
 
6
- # Load data.json
7
- with open('data.json') as f:
8
- data = json.load(f)
 
 
 
9
 
10
  client_endpoint = "https://olivier-truong-mistral-super-fast.hf.space/"
11
 
12
  def ask_question(question):
13
- if question in data:
14
- return data[question]
 
 
 
 
15
  else:
16
  client = Client(client_endpoint)
17
  result = client.predict(
@@ -24,9 +32,11 @@ def ask_question(question):
24
  )
25
  answer = result
26
  answer = answer[:-4]
27
- data[question] = answer
28
- with open('data.json', 'w') as f:
29
- json.dump(data, f)
 
 
30
  return answer
31
 
32
  def typewriter(text: str, speed: int):
@@ -39,7 +49,7 @@ def typewriter(text: str, speed: int):
39
 
40
  def main():
41
  st.title("Near Instant Question Answering")
42
- st.write("Limitations: May generate unhelpfull or outdated content, no chat history.")
43
 
44
  question = st.text_input("Ask your question:")
45
 
@@ -55,3 +65,4 @@ def main():
55
 
56
  if __name__ == "__main__":
57
  main()
 
 
1
  import streamlit as st
2
+ from pymongo import MongoClient
3
  from gradio_client import Client
4
  import time
5
+ import os
6
 
7
+ # Connect to MongoDB
8
+ password = os.getenv("password")
9
+ uri = f"mongodb+srv://E:{password}@cluster0.rvt8psd.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
10
+ client = MongoClient(uri)
11
+ db = client["instantqa"]
12
+ collection = db["instantqa"]
13
 
14
  client_endpoint = "https://olivier-truong-mistral-super-fast.hf.space/"
15
 
16
  def ask_question(question):
17
+ # Check if the question exists in the database
18
+ query = {"question": question}
19
+ result = collection.find_one(query)
20
+
21
+ if result:
22
+ return result["answer"]
23
  else:
24
  client = Client(client_endpoint)
25
  result = client.predict(
 
32
  )
33
  answer = result
34
  answer = answer[:-4]
35
+
36
+ # Store the question-answer pair in MongoDB
37
+ data = {"question": question, "answer": answer}
38
+ collection.insert_one(data)
39
+
40
  return answer
41
 
42
  def typewriter(text: str, speed: int):
 
49
 
50
  def main():
51
  st.title("Near Instant Question Answering")
52
+ st.write("Limitations: May generate unhelpful or outdated content, no chat history.")
53
 
54
  question = st.text_input("Ask your question:")
55
 
 
65
 
66
  if __name__ == "__main__":
67
  main()
68
+