|
import streamlit as st |
|
import requests |
|
import tempfile |
|
import validators |
|
import os |
|
|
|
|
|
with open('styles.css') as f: |
|
css = f.read() |
|
|
|
st.markdown(f'<style>{css}</style>', unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
def save_uploaded_file(uploaded_file): |
|
file_content = uploaded_file.read() |
|
|
|
|
|
data_dir = "/data" |
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, dir=data_dir) as temp_file: |
|
temp_file.write(file_content) |
|
temp_file_path = temp_file.name |
|
return temp_file_path |
|
|
|
|
|
def save_uploaded_image(uploaded_image): |
|
|
|
images_dir = "/images" |
|
|
|
|
|
|
|
temp_file_path = os.path.join(images_dir, tempfile.NamedTemporaryFile(suffix=".png").name) |
|
|
|
|
|
with open(temp_file_path, "wb") as temp_file: |
|
temp_file.write(uploaded_image.read()) |
|
return temp_file_path |
|
|
|
|
|
|
|
|
|
col1, col2 = st.columns([1, 4]) |
|
with col1: |
|
st.image("brainbot.png", use_column_width=True,) |
|
with col2: |
|
st.title("Hi, I am BrainBot - Your AI Learning Assistant!") |
|
|
|
|
|
st.header("Upload any π file, πΌοΈ image, or π webpage link and ask me anything from it!") |
|
st.subheader("Supported file formats: PDF, DOCX, TXT, PPTX, HTML") |
|
st.subheader("Supported image formats: PNG, JPG, JPEG") |
|
|
|
col3, col4 = st.columns([2, 3]) |
|
with col3: |
|
|
|
|
|
llm = st.radio( |
|
"Choose the LLM", ["GPT-4", "GROQ"], |
|
index=1 |
|
) |
|
|
|
st.session_state["llm"] = llm |
|
|
|
|
|
|
|
|
|
uploaded_file = None |
|
uploaded_image = None |
|
website_link = None |
|
question = None |
|
|
|
if llm == "GPT-4" and "api_key_flag" not in st.session_state: |
|
st.warning("Please enter your OpenAI API key.") |
|
|
|
openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password") |
|
|
|
|
|
with st.spinner("Activating OpenAI API..."): |
|
try: |
|
FASTAPI_URL = "http://localhost:7860/set_api_key" |
|
data = {"api_key": openai_api_key} |
|
if openai_api_key: |
|
response = requests.post(FASTAPI_URL, json=data) |
|
st.sidebar.success(response.text) |
|
st.session_state['api_key_flag'] = True |
|
st.experimental_rerun() |
|
except Exception as e: |
|
st.switch_page("pages/error.py") |
|
with col4: |
|
if llm == "GROQ" or "api_key_flag" in st.session_state: |
|
|
|
upload_option = st.radio( |
|
"Select an option", ["π Upload File", "πΌοΈ Upload Image", "π Upload Link"] |
|
) |
|
|
|
if upload_option == "π Upload File": |
|
uploaded_file = st.file_uploader("Choose a file", |
|
type=["txt", "pdf", "docx", "pptx", "html"]) |
|
elif upload_option == "πΌοΈ Upload Image": |
|
uploaded_image = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"]) |
|
elif upload_option == "π Upload Link": |
|
website_link = st.text_input("Enter a website URL") |
|
|
|
|
|
|
|
|
|
if 'file_chat_history' not in st.session_state: |
|
st.session_state['file_chat_history'] = [] |
|
|
|
if 'image_chat_history' not in st.session_state: |
|
st.session_state['image_chat_history'] = [] |
|
|
|
if 'web_chat_history' not in st.session_state: |
|
st.session_state['web_chat_history'] = [] |
|
|
|
|
|
|
|
|
|
|
|
st.session_state['uploaded_file'] = False |
|
if uploaded_file is not None: |
|
with st.spinner("Loading file..."): |
|
|
|
temp_file_path = save_uploaded_file(uploaded_file) |
|
|
|
try: |
|
|
|
data = {"file_path": temp_file_path, "file_type": uploaded_file.type} |
|
FASTAPI_URL = f"http://localhost:7860/load_file/{llm}" |
|
st.write(FASTAPI_URL) |
|
response = requests.post(FASTAPI_URL, json=data) |
|
st.success(response.text) |
|
st.session_state['current_file'] = uploaded_file.name |
|
st.session_state['uploaded_file'] = True |
|
st.switch_page("pages/File-chat.py") |
|
except Exception as e: |
|
st.switch_page("pages/error.py") |
|
|
|
|
|
|
|
|
|
st.session_state['uploaded_image'] = False |
|
if uploaded_image is not None: |
|
try: |
|
|
|
temp_img_path = save_uploaded_image(uploaded_image) |
|
except Exception as e: |
|
st.switch_page("pages/error.py") |
|
|
|
st.session_state['temp_img_path'] = temp_img_path |
|
st.session_state['current_image'] = uploaded_image.name |
|
st.session_state['uploaded_image'] = True |
|
st.switch_page("pages/Image-scan.py") |
|
|
|
|
|
|
|
|
|
|
|
st.session_state['uploaded_link'] = False |
|
if website_link is not None: |
|
if website_link: |
|
|
|
if validators.url(website_link): |
|
try: |
|
|
|
|
|
FASTAPI_URL = f"http://localhost:7860/load_link/{llm}" |
|
data = {"website_link": website_link} |
|
with st.spinner("Loading website..."): |
|
response = requests.post(FASTAPI_URL, json=data) |
|
st.success(response.text) |
|
st.session_state['current_website'] = website_link |
|
st.session_state['uploaded_link'] = True |
|
st.switch_page("pages/Web-chat.py") |
|
except Exception as e: |
|
st.switch_page("pages/error.py") |
|
else: |
|
st.error("Invalid URL. Please enter a valid URL.") |