M17idd commited on
Commit
3cae0f2
·
verified ·
1 Parent(s): 7195b4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -19
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import time
3
  import streamlit as st
4
  from langchain.document_loaders import PyPDFLoader
@@ -10,10 +9,6 @@ from langchain.chains import RetrievalQA
10
  from langchain.chat_models import ChatOpenAI
11
  from typing import List
12
  from together import Together
13
- from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
14
-
15
- import streamlit as st
16
- from PIL import Image
17
 
18
  st.set_page_config(page_title="چت‌ بات توانا", page_icon="🪖", layout="wide")
19
 
@@ -21,19 +16,16 @@ st.set_page_config(page_title="چت‌ بات توانا", page_icon="🪖", lay
21
  st.markdown("""
22
  <style>
23
  @import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700&display=swap');
24
-
25
  html, body, [class*="css"] {
26
  font-family: 'Vazirmatn', Tahoma, sans-serif;
27
  direction: rtl;
28
  text-align: right;
29
  }
30
-
31
  .stApp {
32
  background: url("military_bg.jpeg") no-repeat center center fixed;
33
  background-size: cover;
34
  backdrop-filter: blur(2px);
35
  }
36
-
37
  .stChatMessage {
38
  background-color: rgba(255,255,255,0.8);
39
  border: 1px solid #4e8a3e;
@@ -43,7 +35,6 @@ st.markdown("""
43
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
44
  animation: fadeIn 0.4s ease-in-out;
45
  }
46
-
47
  .stTextInput > div > input, .stTextArea textarea {
48
  background-color: rgba(255,255,255,0.9) !important;
49
  border-radius: 8px !important;
@@ -51,7 +42,6 @@ st.markdown("""
51
  text-align: right;
52
  font-family: 'Vazirmatn', Tahoma;
53
  }
54
-
55
  .stButton>button {
56
  background-color: #4e8a3e !important;
57
  color: white !important;
@@ -60,11 +50,9 @@ st.markdown("""
60
  padding: 8px 20px;
61
  transition: 0.3s;
62
  }
63
-
64
  .stButton>button:hover {
65
  background-color: #3c6d30 !important;
66
  }
67
-
68
  .header-text {
69
  text-align: center;
70
  margin-top: 20px;
@@ -74,20 +62,17 @@ st.markdown("""
74
  border-radius: 20px;
75
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
76
  }
77
-
78
  .header-text h1 {
79
  font-size: 42px;
80
  color: #2c3e50;
81
  margin: 0;
82
  font-weight: bold;
83
  }
84
-
85
  .subtitle {
86
  font-size: 18px;
87
  color: #34495e;
88
  margin-top: 8px;
89
  }
90
-
91
  @keyframes fadeIn {
92
  from { opacity: 0; transform: translateY(10px); }
93
  to { opacity: 1; transform: translateY(0); }
@@ -112,8 +97,6 @@ st.markdown("""
112
  </div>
113
  """, unsafe_allow_html=True)
114
  embeddings = OpenAIEmbeddings()
115
-
116
-
117
  class TogetherEmbeddings(Embeddings):
118
  def __init__(self, model_name: str, api_key: str):
119
  self.model_name = model_name
@@ -130,7 +113,10 @@ class TogetherEmbeddings(Embeddings):
130
  def get_pdf_index():
131
  with st.spinner('📄 در حال پردازش فایل PDF...'):
132
  loader = [PyPDFLoader('test1.pdf')]
133
- embeddings = HuggingFaceInstructEmbeddings(model_name="SajjadAyoubi/xlm-roberta-large-fa-qa")
 
 
 
134
  return VectorstoreIndexCreator(
135
  embedding=embeddings,
136
  text_splitter=RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=0)
@@ -173,7 +159,7 @@ if st.session_state.pending_prompt:
173
  thinking = st.empty()
174
  thinking.markdown("🤖 در حال فکر کردن...")
175
 
176
- response = chain.run(f'question:پاسخ را فقط به زبان فارسی جواب بده {st.session_state.pending_prompt}')
177
  answer = response.split("Helpful Answer:")[-1].strip()
178
  if not answer:
179
  answer = "متأسفم، اطلاعات دقیقی در این مورد ندارم."
 
 
1
  import time
2
  import streamlit as st
3
  from langchain.document_loaders import PyPDFLoader
 
9
  from langchain.chat_models import ChatOpenAI
10
  from typing import List
11
  from together import Together
 
 
 
 
12
 
13
  st.set_page_config(page_title="چت‌ بات توانا", page_icon="🪖", layout="wide")
14
 
 
16
  st.markdown("""
17
  <style>
18
  @import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700&display=swap');
 
19
  html, body, [class*="css"] {
20
  font-family: 'Vazirmatn', Tahoma, sans-serif;
21
  direction: rtl;
22
  text-align: right;
23
  }
 
24
  .stApp {
25
  background: url("military_bg.jpeg") no-repeat center center fixed;
26
  background-size: cover;
27
  backdrop-filter: blur(2px);
28
  }
 
29
  .stChatMessage {
30
  background-color: rgba(255,255,255,0.8);
31
  border: 1px solid #4e8a3e;
 
35
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
36
  animation: fadeIn 0.4s ease-in-out;
37
  }
 
38
  .stTextInput > div > input, .stTextArea textarea {
39
  background-color: rgba(255,255,255,0.9) !important;
40
  border-radius: 8px !important;
 
42
  text-align: right;
43
  font-family: 'Vazirmatn', Tahoma;
44
  }
 
45
  .stButton>button {
46
  background-color: #4e8a3e !important;
47
  color: white !important;
 
50
  padding: 8px 20px;
51
  transition: 0.3s;
52
  }
 
53
  .stButton>button:hover {
54
  background-color: #3c6d30 !important;
55
  }
 
56
  .header-text {
57
  text-align: center;
58
  margin-top: 20px;
 
62
  border-radius: 20px;
63
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
64
  }
 
65
  .header-text h1 {
66
  font-size: 42px;
67
  color: #2c3e50;
68
  margin: 0;
69
  font-weight: bold;
70
  }
 
71
  .subtitle {
72
  font-size: 18px;
73
  color: #34495e;
74
  margin-top: 8px;
75
  }
 
76
  @keyframes fadeIn {
77
  from { opacity: 0; transform: translateY(10px); }
78
  to { opacity: 1; transform: translateY(0); }
 
97
  </div>
98
  """, unsafe_allow_html=True)
99
  embeddings = OpenAIEmbeddings()
 
 
100
  class TogetherEmbeddings(Embeddings):
101
  def __init__(self, model_name: str, api_key: str):
102
  self.model_name = model_name
 
113
  def get_pdf_index():
114
  with st.spinner('📄 در حال پردازش فایل PDF...'):
115
  loader = [PyPDFLoader('test1.pdf')]
116
+ embeddings = TogetherEmbeddings(
117
+ model_name="togethercomputer/m2-bert-80M-8k-retrieval",
118
+ api_key="0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979"
119
+ )
120
  return VectorstoreIndexCreator(
121
  embedding=embeddings,
122
  text_splitter=RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=0)
 
159
  thinking = st.empty()
160
  thinking.markdown("🤖 در حال فکر کردن...")
161
 
162
+ response = chain.run(f'پاسخ را فقط به زبان فارسی جواب بده. سوال: {st.session_state.pending_prompt}')
163
  answer = response.split("Helpful Answer:")[-1].strip()
164
  if not answer:
165
  answer = "متأسفم، اطلاعات دقیقی در این مورد ندارم."