import os
from typing import Dict, List
import gradio as gr
from src.main import get_response, add_file
from dotenv import load_dotenv
load_dotenv("./.env")
print(os.environ)
def process_input(message: str, history: List):
response = get_response(message)
history.append((message, response["answer"]))
return (
"",
history,
"\n".join([res.metadata["source"] for res in response["source_documents"]]),
)
def add_to_db(file: gr.File):
add_file(file)
gr.Info("File added to Vector Database")
def main():
with gr.Blocks(title="Alialhajri Chat PPG", theme="soft") as demo:
gr.HTML(
"""
"""
)
with gr.Tabs():
with gr.Tab(label="Chatbot"):
chatbot = gr.Chatbot(height=300, likeable=True)
message = gr.Textbox(
placeholder="Ask me a yes or no question",
scale=7,
label="Input message",
)
sources = gr.Textbox(label="Sources")
_ = gr.ClearButton([message, chatbot, sources])
message.submit(
process_input,
inputs=[message, chatbot],
outputs=[message, chatbot, sources],
)
# with gr.Tab(label="Add Data"):
# upload_file = gr.File(label="Upload file(PDF, DOCX, Image)")
# upload_file_button = gr.Button(value="Add File to Vector DB")
# upload_file_button.click(add_to_db, inputs=upload_file)
demo.launch(allowed_paths=["static", "."])
if __name__ == "__main__":
main()