Really-amin commited on
Commit
0489530
·
verified ·
1 Parent(s): dd5083c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -1,34 +1,52 @@
1
  import streamlit as st
2
- from transformers import pipeline, set_seed
3
- import json
4
 
5
- # بارگذاری مدل GPT-2
6
  @st.cache_resource
7
- def load_model():
8
- generator = pipeline("text-generation", model="gpt2")
9
- set_seed(42) # برای تکرارپذیری پاسخ‌ها
10
- return generator
11
 
12
- model = load_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # اپلیکیشن Streamlit
15
  def main():
16
  st.set_page_config(page_title="دستیار هوش مصنوعی", layout="wide")
17
  st.title("دستیار هوش مصنوعی پیشرفته")
18
 
19
- # نمایش فایل HTML
20
- with open("index.html", "r", encoding="utf-8") as html_file:
21
- html_content = html_file.read()
22
- st.components.v1.html(html_content, height=800, scrolling=True)
23
-
24
- # مدیریت درخواست‌های API
25
- st.subheader("مدیریت درخواست‌ها")
26
- input_data = st.text_input("پیام دریافتی (برای تست):", "")
27
- if st.button("تست"):
28
- if input_data.strip():
29
- st.write("پیام دریافتی:", input_data)
30
- response = model(input_data, max_length=50, num_return_sequences=1)
31
- st.write("پاسخ مدل:", response[0]["generated_text"])
32
 
33
  if __name__ == "__main__":
34
  main()
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ import torch
4
 
5
+ # بارگذاری مدل با pipeline
6
  @st.cache_resource
7
+ def load_model_pipeline():
8
+ return pipeline("text-generation", model="openai-community/gpt2")
 
 
9
 
10
+ # یا بارگذاری مدل با AutoTokenizer و AutoModelForCausalLM
11
+ @st.cache_resource
12
+ def load_model_direct():
13
+ tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
14
+ model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
15
+ return tokenizer, model
16
+
17
+ # انتخاب روش
18
+ USE_PIPELINE = True # True برای استفاده از pipeline، False برای استفاده مستقیم
19
+
20
+ if USE_PIPELINE:
21
+ model = load_model_pipeline()
22
+ else:
23
+ tokenizer, model = load_model_direct()
24
+
25
+ # تابع تولید پاسخ
26
+ def generate_response(prompt):
27
+ if USE_PIPELINE:
28
+ response = model(prompt, max_length=50, num_return_sequences=1)
29
+ return response[0]["generated_text"]
30
+ else:
31
+ inputs = tokenizer(prompt, return_tensors="pt")
32
+ outputs = model.generate(inputs["input_ids"], max_length=50, num_return_sequences=1)
33
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
34
 
35
  # اپلیکیشن Streamlit
36
  def main():
37
  st.set_page_config(page_title="دستیار هوش مصنوعی", layout="wide")
38
  st.title("دستیار هوش مصنوعی پیشرفته")
39
 
40
+ # ورودی پیام
41
+ user_input = st.text_input("پیام خود را وارد کنید:")
42
+ if st.button("ارسال"):
43
+ if user_input.strip():
44
+ with st.spinner("در حال پردازش..."):
45
+ response = generate_response(user_input)
46
+ st.success("پاسخ مدل:")
47
+ st.write(response)
48
+ else:
49
+ st.warning("لطفاً یک پیام وارد کنید.")
 
 
 
50
 
51
  if __name__ == "__main__":
52
  main()