Soumen commited on
Commit
f1ebc19
·
1 Parent(s): 0a75d54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -65
app.py CHANGED
@@ -85,7 +85,8 @@ def bansum(text):
85
 
86
  if "photo" not in st.session_state:
87
  st.session_state["photo"]="not done"
88
- c2, c3 = st.columns([2,1])
 
89
  def change_photo_state():
90
  st.session_state["photo"]="done"
91
  @st.cache
@@ -93,71 +94,108 @@ def save(l):
93
  return l
94
  #@st.cache
95
  def main():
96
- message = st.text_input("Type your text here!")
97
- camera_photo = c2.camera_input("Capture a photo to summarize: ", on_change=change_photo_state)
98
- uploaded_photo = save(c3.file_uploader("Upload your Images/PDF",type=['jpg','png','jpeg','pdf'], on_change=change_photo_state))
99
- if st.session_state["photo"]=="done" or message:
100
- if uploaded_photo and uploaded_photo.type=='application/pdf':
101
- tet = read_pdf(uploaded_photo)
102
- # with tempfile.NamedTemporaryFile(delete=False) as temp_file:
103
- # temp_file.write(uploaded_photo.read())
104
- # temp_file_path = temp_file.name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
- # loader = PyPDFLoader(temp_file_path)
107
- # if loader:
108
- # text.extend(loader.load())
109
- # os.remove(temp_file_path)
110
- # text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=100, length_function=len)
111
- # text_chunks = text_splitter.split_documents(text)
112
- values = st.slider('Select a approximate number of lines to see and summarize',value=[0, len(tet)//(7*100)])
113
- text = tet[values[0]*7*10:values[1]*10*100] if values[0]!=len(tet)//(10*100) else tet[len(tet)//(10*100):]
114
- #st.success(type(text_chunks))
115
- if st.button("English Pdf Summarize"):
116
- st.subheader("Selected text for summarize: ")
117
- st.success(text)
118
- st.subheader("Summarized Text: ")
119
- engsum(text)
120
-
121
- elif uploaded_photo and uploaded_photo.type !='application/pdf':
122
- text=None
123
- img = Image.open(uploaded_photo)
124
- img = img.save("img.png")
125
- img = cv2.imread("img.png")
126
- st.text("Select the summarization type:")
127
- c4, c5 = st.columns([1,1])
128
- if c4.button("BENGALI"):
129
- text = pytesseract.image_to_string(img, lang="ben")
130
- st.subheader("সারাংশ/সারমর্ম")
131
- bansum(text)
132
- if c5.button("ENGLISH"):
133
- text=pytesseract.image_to_string(img)
134
- st.subheader("Summarized Text")
135
- engsum(text)
136
- #st.success(text)
137
- elif camera_photo:
138
- text=None
139
- img = Image.open(camera_photo)
140
- img = img.save("img.png")
141
- img = cv2.imread("img.png")
142
- #text = pytesseract.image_to_string(img) if st.checkbox("Bangla") else pytesseract.image_to_string(img, lang="ben")
143
- st.text("Select the summarization type:")
144
- c6, c7 = st.columns([1,1])
145
- if c6.button("Bangla"):
146
- text = pytesseract.image_to_string(img, lang="ben")
147
- st.subheader("সারাংশ/সারমর্ম")
148
- bansum(text)
149
- if c7.button("English"):
150
- text=pytesseract.image_to_string(img)
151
- st.subheader("Summarized Text")
152
- engsum(text)
153
- else:
154
- text=None
155
- text = message
156
- c8, c9 = st.columns([1,1])
157
- if c8.button("Bangla"):
158
- bansum(text)
159
- if c9.button("English"):
160
- engsum(text)
161
  # if st.button("English Text Generation"):
162
  # def query(payload):
163
  # response = requests.post(API_URL2, headers=headers2, json=payload)
 
85
 
86
  if "photo" not in st.session_state:
87
  st.session_state["photo"]="not done"
88
+ c2, c3 = st.columns([1,1])
89
+ a, b = st.columns([1, 1])
90
  def change_photo_state():
91
  st.session_state["photo"]="done"
92
  @st.cache
 
94
  return l
95
  #@st.cache
96
  def main():
97
+ with st.container():
98
+ with a:
99
+ #import torch
100
+ import streamlit as st
101
+ from streamlit_option_menu import option_menu
102
+ from streamlit_chat import message as st_message
103
+ from transformers import BlenderbotTokenizer
104
+ from transformers import BlenderbotForConditionalGeneration
105
+ st.title("Simple Chatbot for fun!")
106
+
107
+ @st.experimental_singleton
108
+ def get_models():
109
+ # it may be necessary for other frameworks to cache the model
110
+ # seems pytorch keeps an internal state of the conversation
111
+ model_name = "facebook/blenderbot-400M-distill"
112
+ tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
113
+ model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
114
+ return tokenizer, model
115
+ if "history" not in st.session_state:
116
+ st.session_state.history = []
117
+ st.title("Hello Chatbot")
118
+ def main():
119
+ st.text_input("Talk to the bot", key="input_text", on_change=generate_answer)
120
+ def generate_answer():
121
+ tokenizer, model = get_models()
122
+ user_message = st.session_state.input_text
123
+ inputs = tokenizer(st.session_state.input_text, return_tensors="pt")
124
+ result = model.generate(**inputs)
125
+ message_bot = tokenizer.decode(
126
+ result[0], skip_special_tokens=True
127
+ ) # .replace("<s>", "").replace("</s>", "")
128
+ st.session_state.history.append({"message": user_message, "is_user": True})
129
+ st.session_state.history.append({"message": message_bot, "is_user": False})
130
+ from copyreg import clear_extension_cache
131
+ for chat in st.session_state.history:
132
+ st_message(**chat)
133
+ with b:
134
+ message = st.text_input("Type your text here!")
135
+ camera_photo = c2.camera_input("Capture a photo to summarize: ", on_change=change_photo_state)
136
+ uploaded_photo = save(c3.file_uploader("Upload your Images/PDF",type=['jpg','png','jpeg','pdf'], on_change=change_photo_state))
137
+ if st.session_state["photo"]=="done" or message:
138
+ if uploaded_photo and uploaded_photo.type=='application/pdf':
139
+ tet = read_pdf(uploaded_photo)
140
+ # with tempfile.NamedTemporaryFile(delete=False) as temp_file:
141
+ # temp_file.write(uploaded_photo.read())
142
+ # temp_file_path = temp_file.name
143
+
144
+ # loader = PyPDFLoader(temp_file_path)
145
+ # if loader:
146
+ # text.extend(loader.load())
147
+ # os.remove(temp_file_path)
148
+ # text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=100, length_function=len)
149
+ # text_chunks = text_splitter.split_documents(text)
150
+ values = st.slider('Select a approximate number of lines to see and summarize',value=[0, len(tet)//(7*100)])
151
+ text = tet[values[0]*7*10:values[1]*10*100] if values[0]!=len(tet)//(10*100) else tet[len(tet)//(10*100):]
152
+ #st.success(type(text_chunks))
153
+ if st.button("English Pdf Summarize"):
154
+ st.subheader("Selected text for summarize: ")
155
+ st.success(text)
156
+ st.subheader("Summarized Text: ")
157
+ engsum(text)
158
 
159
+ elif uploaded_photo and uploaded_photo.type !='application/pdf':
160
+ text=None
161
+ img = Image.open(uploaded_photo)
162
+ img = img.save("img.png")
163
+ img = cv2.imread("img.png")
164
+ st.text("Select the summarization type:")
165
+ c4, c5 = st.columns([1,1])
166
+ if c4.button("BENGALI"):
167
+ text = pytesseract.image_to_string(img, lang="ben")
168
+ st.subheader("সারাংশ/সারমর্ম")
169
+ bansum(text)
170
+ if c5.button("ENGLISH"):
171
+ text=pytesseract.image_to_string(img)
172
+ st.subheader("Summarized Text")
173
+ engsum(text)
174
+ #st.success(text)
175
+ elif camera_photo:
176
+ text=None
177
+ img = Image.open(camera_photo)
178
+ img = img.save("img.png")
179
+ img = cv2.imread("img.png")
180
+ #text = pytesseract.image_to_string(img) if st.checkbox("Bangla") else pytesseract.image_to_string(img, lang="ben")
181
+ st.text("Select the summarization type:")
182
+ c6, c7 = st.columns([1,1])
183
+ if c6.button("Bangla"):
184
+ text = pytesseract.image_to_string(img, lang="ben")
185
+ st.subheader("সারাংশ/সারমর্ম")
186
+ bansum(text)
187
+ if c7.button("English"):
188
+ text=pytesseract.image_to_string(img)
189
+ st.subheader("Summarized Text")
190
+ engsum(text)
191
+ else:
192
+ text=None
193
+ text = message
194
+ c8, c9 = st.columns([1,1])
195
+ if c8.button("Bangla"):
196
+ bansum(text)
197
+ if c9.button("English"):
198
+ engsum(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  # if st.button("English Text Generation"):
200
  # def query(payload):
201
  # response = requests.post(API_URL2, headers=headers2, json=payload)