ai-assistant / app.py
digitalai's picture
Rename main.py to app.py
53fbc3b verified
raw
history blame
No virus
4.41 kB
import time
import streamlit as st
from PIL import Image
from config.quiz import Question
from config.option import Option
from config.valid_text import is_valid_input
from arabic_support import support_arabic_text
from pathlib import Path
import json
st.set_page_config(
page_title="AI-Medical Questionnaire",
page_icon="💊",
layout="wide",
initial_sidebar_state="expanded",
)
# مسیر فایل فعلی
current_file_path = Path(__file__).resolve()
# مسیر ریشه پروژه (دو سطح بالاتر از فایل فعلی)
project_root = current_file_path.parents[0]
# مسیرهای دایرکتوری‌ها
assets_dir = project_root / "asset"
database_dir = project_root / "data"
styles_dir = project_root / "styles"
# پشتیبانی از متن عربی در تمامی اجزاء
support_arabic_text(all=True)
# خواندن فایل CSS
css_file_path = styles_dir / "main.css"
with open(css_file_path, "r", encoding="utf-8") as file:
css_code = file.read()
# اعمال CSS
st.markdown(f"<style>{css_code}</style>", unsafe_allow_html=True)
support_arabic_text(all=True)
state = st.session_state
st.title("AI-Medical Questionnaire")
side = st.sidebar
if "messages" not in state:
state["messages"] = []
if "level" not in state:
state.level = "L01"
state.next = ""
state.previous = ""
# مسیرهای فایل‌های JSON
question_file = database_dir / "question_data.json"
option_file = database_dir / "option_data.json"
# ایجاد کلاس‌های سوالات و گزینه‌ها
question_class = Question(question_file, option_file)
option_class = Option(question_file, option_file)
# بارگذاری داده‌ها
question = question_class.load_question_data()
options = option_class.load_option_data()
question_text = question_class.get_question_text(state.level)
with side:
img = Image.open(assets_dir / "logo.png")
st.image(
img,
width=250,
caption="جهت بازنشانی از دکمه ریست استفاده کنید",
)
if st.button("reset"):
del state["messages"]
st.rerun()
def response_generator():
resp = question_class.get_question_text(state.level)[0]
for word in resp.split():
character = ""
for char in word:
yield character + char
time.sleep(0.02)
yield " "
time.sleep(0.09)
if "start" not in state:
state.start = """ سلام من دستیار شما هستم. برای بررسی و تصمیم گیری وضعیت بیماران مرحله به مرحله بررسی وضعیت بیمار را با کمک من انجام دهید. برای پیشنهاد می کنم
اختلالات حرکتی را بررسی کنید. آیا بیمار دارای چنین اختلالی است؟)"""
st.chat_message("assistant", avatar=str(assets_dir / "robot_icon.png")).markdown(
state.start
)
for messages in state.messages:
with st.chat_message(messages["role"]):
st.markdown(messages["content"])
prompt = st.chat_input("""نوشتن پیام""")
if prompt:
st.chat_message("user", avatar=str(assets_dir / "user.png")).markdown(prompt)
state.messages.append({"role": "user", "content": prompt})
if is_valid_input(prompt) != "wrong value":
prompt = is_valid_input(prompt)
next_level = option_class.get_next_level(prompt, state.level)
feedback = option_class.get_feedback(prompt, state.level)
if next_level != "L98":
my_bar = st.progress(0, text=feedback)
for percent_complete in range(100):
time.sleep(0.01)
my_bar.progress(percent_complete + 1, text=feedback)
time.sleep(1)
my_bar.empty()
state.level = next_level
with st.chat_message("assistant"):
response = st.write_stream(response_generator())
state.messages.append({"role": "assistant", "content": response})
else:
st.switch_page(str(project_root / "pages" / "result.py"))
else:
with st.chat_message("assistant"):
response = st.write(
"با عرض پوزش پاسخ شما به پرسش را متوجه نمیشوم لطفا پاسخ مناسب را وارد کنید"
)
state.messages.append({"role": "assistant", "content": response})