Really-amin commited on
Commit
73a7222
·
verified ·
1 Parent(s): e1823a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -113
app.py CHANGED
@@ -1,120 +1,33 @@
1
- import streamlit as st
2
- from streamlit_chat import message
3
- from transformers import pipeline
4
- import base64
5
-
6
- # تنظیمات اولیه صفحه
7
- st.set_page_config(page_title="دستیار هوش مصنوعی پیشرفته", page_icon="🧠", layout="wide")
8
-
9
- # استایل CSS سفارشی
10
- st.markdown(
11
- """
12
- <style>
13
- @keyframes rotate {
14
- 0% { transform: rotate(0deg); }
15
- 100% { transform: rotate(360deg); }
16
- }
17
- @keyframes float {
18
- 0%, 100% { transform: translateY(0); }
19
- 50% { transform: translateY(-10px); }
20
- }
21
- .brain-icon {
22
- display: flex;
23
- justify-content: center;
24
- align-items: center;
25
- margin-bottom: 20px;
26
- }
27
- .brain-icon img {
28
- width: 100px;
29
- height: 100px;
30
- animation: rotate 10s linear infinite, float 3s ease-in-out infinite;
31
- }
32
- .main {
33
- background-color: #f0f3f5 !important;
34
- font-family: 'Vazirmatn', sans-serif;
35
- }
36
- div[data-testid="stTextArea"] > div {
37
- border-radius: 30px;
38
- border: 2px solid #3498db;
39
- padding: 10px;
40
- }
41
- div[data-testid="stTextArea"] textarea {
42
- font-size: 16px !important;
43
- font-family: 'Vazirmatn', sans-serif;
44
- }
45
- button[data-testid="stButton"] {
46
- background-color: #3498db !important;
47
- color: white !important;
48
- border: none !important;
49
- border-radius: 50px !important;
50
- font-size: 18px !important;
51
- padding: 10px 20px !important;
52
- }
53
- button[data-testid="stButton"]:hover {
54
- background-color: #2980b9 !important;
55
- }
56
- </style>
57
- """,
58
- unsafe_allow_html=True,
59
- )
60
 
61
- # افزودن فونت فارسی (Vazirmatn)
62
- st.markdown(
63
- """
64
- <link href="https://cdn.jsdelivr.net/gh/rastikerdar/vazirmatn@v33.003/Vazirmatn-font-face.css" rel="stylesheet" type="text/css" />
65
- """,
66
- unsafe_allow_html=True,
67
- )
68
-
69
- # نمایش آیکون مغز با انیمیشن
70
- st.markdown(
71
- """
72
- <div class="brain-icon">
73
- <img src="https://upload.wikimedia.org/wikipedia/commons/6/6e/Brain_in_a_jar.svg" alt="Brain Icon">
74
- </div>
75
- """,
76
- unsafe_allow_html=True,
77
- )
78
 
79
- # عنوان صفحه
80
- st.title("🧠 دستیار هوش مصنوعی پیشرفته")
81
 
82
- # حالت جلسه برای ذخیره تاریخچه چت
83
- if "messages" not in st.session_state:
84
- st.session_state.messages = [] # ذخیره پیام‌های کاربر و دستیار
85
 
86
- # اتصال به مدل هوش مصنوعی (GPT از Hugging Face)
87
- @st.cache_resource
88
- def load_model():
89
- return pipeline("text-generation", model="gpt2")
90
 
91
- generator = load_model()
 
 
 
 
 
92
 
93
- # نمایش پیام‌های چت
94
- for msg in st.session_state.messages:
95
- if msg["is_user"]:
96
- message(msg["content"], is_user=True) # پیام کاربر
97
- else:
98
- message(msg["content"], is_user=False) # پیام دستیار
99
 
100
- # ورودی پیام جدید
101
- with st.container():
102
- user_input = st.text_input(
103
- "سوال خود را اینجا بنویسید...",
104
- key="user_input",
105
- placeholder="مثلاً تحلیل قیمت بیت‌کوین...",
106
- label_visibility="collapsed",
107
- )
108
- if st.button("ارسال"):
109
- if user_input.strip(): # اگر پیام خالی نباشد
110
- # افزودن پیام کاربر به تاریخچه
111
- st.session_state.messages.append({"content": user_input, "is_user": True})
112
-
113
- # دریافت پاسخ از مدل
114
- response = generator(user_input, max_length=50, num_return_sequences=1)[0]["generated_text"]
115
-
116
- # افزودن پاسخ دستیار به تاریخچه
117
- st.session_state.messages.append({"content": response, "is_user": False})
118
-
119
- # به‌روزرسانی صفحه
120
- st.experimental_rerun()
 
1
+ """
2
+ این فایل یک سرور ساده Flask اجرا می‌کند که به فایل index.html متصل است.
3
+ مدل GPT-2 از Hugging Face برای تولید پاسخ‌ها استفاده می‌شود.
4
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ from flask import Flask, request, jsonify, render_template
7
+ from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # بارگذاری مدل از Hugging Face
10
+ generator = pipeline("text-generation", model="gpt2")
11
 
12
+ # ایجاد برنامه Flask
13
+ app = Flask(__name__)
 
14
 
15
+ @app.route("/")
16
+ def index():
17
+ return render_template("index.html") # فایل index.html
 
18
 
19
+ @app.route("/predict", methods=["POST"])
20
+ def predict():
21
+ data = request.get_json()
22
+ user_message = data.get("message", "")
23
+ if not user_message.strip():
24
+ return jsonify({"reply": "لطفاً یک سوال وارد کنید!"})
25
 
26
+ # تولید پاسخ با مدل
27
+ response = generator(user_message, max_length=50, num_return_sequences=1)
28
+ reply = response[0]["generated_text"]
29
+ return jsonify({"reply": reply})
 
 
30
 
31
+ # اجرای برنامه
32
+ if __name__ == "__main__":
33
+ app.run(debug=True, host="0.0.0.0", port=8000)