Really-amin commited on
Commit
6527a15
·
verified ·
1 Parent(s): b6b9c84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
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 json
5
 
6
- # بارگذاری مدل هوش مصنوعی
7
  @st.cache_resource
8
  def load_model():
9
- return pipeline("text-generation", model="openai-community/gpt2")
 
 
10
 
11
  model = load_model()
12
 
13
- # نمایش HTML
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
- # دریافت ورودی از HTML
24
- html_code = load_html()
25
- components.html(html_code, height=600, scrolling=True)
26
-
27
- st.text("اطلاعات را از ورودی HTML دریافت می‌کنیم...")
28
-
29
- # API برای دریافت پیام کاربر
30
- if st.experimental_get_query_params().get("generate"):
31
- request = st.experimental_get_query_params()["generate"]
32
- message = json.loads(request).get("message")
33
- if message:
34
- response = model(message, max_length=100, num_return_sequences=1)
35
- st.json({"response": response[0]["generated_text"]})
 
 
 
 
 
 
 
 
 
 
 
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()