Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,46 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline
|
3 |
from streamlit.components.v1 import components
|
4 |
-
import
|
5 |
|
6 |
-
# بارگذاری مدل
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
-
|
|
|
|
|
10 |
|
11 |
model = load_model()
|
12 |
|
13 |
-
#
|
14 |
-
def load_html():
|
15 |
-
with open("index.html", "r", encoding="utf-8") as f:
|
16 |
-
return f.read()
|
17 |
-
|
18 |
-
# استریملیت اپلیکیشن
|
19 |
def main():
|
20 |
st.set_page_config(page_title="دستیار هوش مصنوعی", layout="wide")
|
21 |
-
st.title("دستیار هوش مصنوعی")
|
22 |
-
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
if __name__ == "__main__":
|
38 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline, set_seed
|
3 |
from streamlit.components.v1 import components
|
4 |
+
import os
|
5 |
|
6 |
+
# بارگذاری مدل GPT-2
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
+
generator = pipeline("text-generation", model="gpt2")
|
10 |
+
set_seed(42) # برای تکرارپذیری پاسخها
|
11 |
+
return generator
|
12 |
|
13 |
model = load_model()
|
14 |
|
15 |
+
# تابع اصلی برنامه Streamlit
|
|
|
|
|
|
|
|
|
|
|
16 |
def main():
|
17 |
st.set_page_config(page_title="دستیار هوش مصنوعی", layout="wide")
|
18 |
+
st.title("دستیار هوش مصنوعی پیشرفته")
|
19 |
+
|
20 |
+
# بارگذاری فایل HTML
|
21 |
+
html_path = "static/index.html"
|
22 |
+
if not os.path.exists(html_path):
|
23 |
+
st.error("فایل HTML پیدا نشد. لطفاً مسیر فایل را بررسی کنید.")
|
24 |
+
return
|
25 |
+
|
26 |
+
with open(html_path, "r", encoding="utf-8") as file:
|
27 |
+
html_content = file.read()
|
28 |
+
|
29 |
+
# نمایش فایل HTML
|
30 |
+
components.html(html_content, height=800, scrolling=True)
|
31 |
+
|
32 |
+
# تعامل با مدل
|
33 |
+
st.subheader("ارتباط با مدل")
|
34 |
+
user_input = st.text_input("پیام خود را وارد کنید:")
|
35 |
+
if st.button("ارسال"):
|
36 |
+
if user_input.strip():
|
37 |
+
# تولید پاسخ با مدل
|
38 |
+
with st.spinner("در حال تولید پاسخ..."):
|
39 |
+
response = model(user_input, max_length=50, num_return_sequences=1)
|
40 |
+
st.success("پاسخ مدل:")
|
41 |
+
st.write(response[0]["generated_text"])
|
42 |
+
else:
|
43 |
+
st.warning("لطفاً یک پیام وارد کنید.")
|
44 |
|
45 |
if __name__ == "__main__":
|
46 |
main()
|