|
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( |
|
""" |
|
<div style="display: flex; flex-direction: column; align-items: center; text-align: center; margin-top: 10px;"> |
|
<div style="width: 100px; height: 100px; border-radius: 50%; overflow: hidden;"> |
|
<img src="/file=static/icon_2.jpg" alt="Hospital Icon" style="width: 100%; height: 100%; object-fit: cover;"> |
|
</div> |
|
<div style="margin-top: 5px; font-size:24px; font-weight: bold;">Hospital Policy</div> |
|
</div> |
|
""" |
|
) |
|
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], |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
demo.launch(allowed_paths=["static", "."]) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |