Spaces:
Running
Running
Update app.py
Browse files
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
|
6 |
|
7 |
-
#
|
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 |
-
|
18 |
-
def
|
19 |
-
|
20 |
-
|
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 |
-
#
|
29 |
-
def
|
30 |
-
st.set_page_config(page_title="
|
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 |
-
#
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
-
|
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()
|
|