My_Ai / app.py
Really-amin's picture
Update app.py
6527a15 verified
raw
history blame
1.61 kB
import streamlit as st
from transformers import pipeline, set_seed
from streamlit.components.v1 import components
import os
# بارگذاری مدل GPT-2
@st.cache_resource
def load_model():
generator = pipeline("text-generation", model="gpt2")
set_seed(42) # برای تکرارپذیری پاسخ‌ها
return generator
model = load_model()
# تابع اصلی برنامه Streamlit
def main():
st.set_page_config(page_title="دستیار هوش مصنوعی", layout="wide")
st.title("دستیار هوش مصنوعی پیشرفته")
# بارگذاری فایل HTML
html_path = "static/index.html"
if not os.path.exists(html_path):
st.error("فایل HTML پیدا نشد. لطفاً مسیر فایل را بررسی کنید.")
return
with open(html_path, "r", encoding="utf-8") as file:
html_content = file.read()
# نمایش فایل HTML
components.html(html_content, height=800, scrolling=True)
# تعامل با مدل
st.subheader("ارتباط با مدل")
user_input = st.text_input("پیام خود را وارد کنید:")
if st.button("ارسال"):
if user_input.strip():
# تولید پاسخ با مدل
with st.spinner("در حال تولید پاسخ..."):
response = model(user_input, max_length=50, num_return_sequences=1)
st.success("پاسخ مدل:")
st.write(response[0]["generated_text"])
else:
st.warning("لطفاً یک پیام وارد کنید.")
if __name__ == "__main__":
main()