File size: 1,433 Bytes
0a533a6
 
 
c79e14b
0a533a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244d820
 
 
 
 
 
 
c79e14b
0a533a6
 
 
 
 
 
 
c79e14b
 
0a533a6
c79e14b
244d820
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
import streamlit as st
import requests
import json
import time

# Load data.json
with open('data.json') as f:
    data = json.load(f)

client_endpoint = "https://olivier-truong-mistral-super-fast.hf.space/chat"

def ask_question(question):
    if question in data:
        return data[question]
    else:
        response = requests.post(client_endpoint, json={"context": question})
        if response.status_code == 200:
            answer = response.json()['response']
            data[question] = answer
            with open('data.json', 'w') as f:
                json.dump(data, f)
            return answer
        else:
            return "Error: Failed to retrieve an 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)

def main():
    st.title("Question Answering System")

    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=10)
        else:
            st.write("Please enter a question.")

if __name__ == "__main__":
    main()