File size: 2,247 Bytes
0a533a6
c310176
cd93ac8
f1a9d95
c310176
0a533a6
c310176
 
 
 
 
 
0a533a6
50d6672
0a533a6
 
c310176
 
 
 
 
 
0a533a6
cd93ac8
 
 
 
 
 
 
 
 
2e81294
651d41d
c310176
 
 
 
 
cd93ac8
0a533a6
244d820
 
 
 
 
 
 
aa1f96b
3d1a501
c79e14b
0a533a6
b855aa8
c310176
ea33258
0a533a6
 
 
 
 
c79e14b
 
0a533a6
c79e14b
a0df0a6
0a533a6
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
from pymongo import MongoClient
from gradio_client import Client
import time
import os

# Connect to MongoDB
password = os.getenv("password")
uri = f"mongodb+srv://E:{password}@cluster0.rvt8psd.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
client = MongoClient(uri)
db = client["instantqa"]
collection = db["instantqa"]

client_endpoint = "https://skier8402-mistral-super-fast.hf.space/"

def ask_question(question):
    # Check if the question exists in the database
    query = {"question": question}
    result = collection.find_one(query)

    if result:
        return result["answer"]
    else:
        client = Client(client_endpoint)
        result = client.predict(
            question,
            0.9,  # Temperature
            2000,  # Max new tokens
            0.9,  # Top-p (nucleus sampling)
            1.2,  # Repetition penalty
            api_name="/chat"
        )
        answer = result
        answer = answer[:-4]
        
        # Store the question-answer pair in MongoDB
        data = {"question": question, "answer": answer}
        collection.insert_one(data)

        return answer

def typewriter(text: str, speed: int):
    tokens = text.split()
    container = st.empty()
    for index in range(len(tokens) + 1):
        curr_full_text = " ".join(tokens[:index])
        container.markdown(curr_full_text)
        time.sleep(1 / speed)
        # JavaScript to scroll to the bottom of the page while typing
        st.write(f"<script>window.scrollTo(0, document.body.scrollHeight);</script>", unsafe_allow_html=True)

def main():
    st.title("Near Instant Question Answering")
    st.write("Limitations: May generate unhelpful or outdated content, no chat history.")
    st.write("Warning: Do not share any personal/public information.")

    question = st.text_input("Ask your question:")

    if st.button("Ask"):
        if question:
            answer_placeholder = st.empty()
            answer_placeholder.write("Thinking...")
            answer = ask_question(question)
            answer_placeholder.empty()
            typewriter("Answer: " + answer, speed=300)
        else:
            st.write("Please enter a question.")

if __name__ == "__main__":
    main()