Really-amin commited on
Commit
1ca069a
·
verified ·
1 Parent(s): 5316073

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
app.py CHANGED
@@ -1,44 +1,38 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
- from flask import Flask, request, jsonify
4
  from streamlit.components.v1 import components
5
- import threading
6
 
7
- # Flask App for API
8
- app = Flask(__name__)
9
-
10
- # Load the AI model
11
  @st.cache_resource
12
  def load_model():
13
  return pipeline("text-generation", model="openai-community/gpt2")
14
 
15
  model = load_model()
16
 
17
- @app.route("/api/chat", methods=["POST"])
18
- def chat():
19
- data = request.json
20
- user_message = data.get("message", "")
21
- if not user_message:
22
- return jsonify({"response": "پیامی وارد نشده است!"})
23
-
24
- # Generate response
25
- response = model(user_message, max_length=50, num_return_sequences=1)
26
- return jsonify({"response": response[0]["generated_text"]})
27
 
28
- # Streamlit app
29
- def start_streamlit():
30
- st.set_page_config(page_title="AI Assistant")
31
  st.title("دستیار هوش مصنوعی")
32
-
33
- with open("static/index.html", "r", encoding="utf-8") as f:
34
- html_content = f.read()
35
-
36
- components.html(html_content, height=800)
37
 
38
- # Start Flask in a separate thread
39
- def start_flask():
40
- app.run(port=5000, debug=False, use_reloader=False)
 
 
 
 
 
 
 
 
 
 
41
 
42
  if __name__ == "__main__":
43
- threading.Thread(target=start_flask).start()
44
- start_streamlit()
 
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()