Spaces:
Runtime error
Runtime error
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 animate_typing(text): | |
for char in text: | |
st.write_stream("Answer: " + text[:text.index(char)+1]) | |
time.sleep(0.05) | |
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() | |
animate_typing(answer) | |
else: | |
st.write("Please enter a question.") | |
if __name__ == "__main__": | |
main() | |