Add application file
Browse files- Untitled.ipynb +425 -0
- account.py +178 -0
- app.py +50 -0
- fyp-streamlit-29af1-170f7ef6aa70.json +13 -0
- history.py +37 -0
- ocr.py +242 -0
- output.mp3 +0 -0
- packages.txt +1 -0
- requirements.txt +5 -0
Untitled.ipynb
ADDED
@@ -0,0 +1,425 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 4,
|
6 |
+
"id": "a36b5482-79ee-47d2-8100-09078990f450",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [
|
9 |
+
{
|
10 |
+
"name": "stdout",
|
11 |
+
"output_type": "stream",
|
12 |
+
"text": [
|
13 |
+
"Overwriting test.py\n"
|
14 |
+
]
|
15 |
+
}
|
16 |
+
],
|
17 |
+
"source": [
|
18 |
+
"\n",
|
19 |
+
"%%writefile test.py\n",
|
20 |
+
"import streamlit as st\n",
|
21 |
+
"import easyocr\n",
|
22 |
+
"from PIL import Image\n",
|
23 |
+
"import numpy as np\n",
|
24 |
+
"from gtts import gTTS\n",
|
25 |
+
"st.sidebar.title('Language Selection Menu')\n",
|
26 |
+
"st.sidebar.subheader('Select Source Language')\n",
|
27 |
+
"source_language = st.sidebar.selectbox(\"Source Language\", ['English', 'Nepali'])\n",
|
28 |
+
"def app():\n",
|
29 |
+
" def display_text(bounds):\n",
|
30 |
+
" text = []\n",
|
31 |
+
" for x in bounds:\n",
|
32 |
+
" t = x[1]\n",
|
33 |
+
" text.append(t)\n",
|
34 |
+
" text = ' '.join(text)\n",
|
35 |
+
" return text\n",
|
36 |
+
" \n",
|
37 |
+
" st.set_option('deprecation.showfileUploaderEncoding', False)\n",
|
38 |
+
" st.title('OCR - Optical Character Recognition')\n",
|
39 |
+
" st.subheader('Extract text from images')\n",
|
40 |
+
" \n",
|
41 |
+
" image_file = st.file_uploader(\"Upload Image\", type=['jpg', 'png', 'jpeg', 'JPG'])\n",
|
42 |
+
" \n",
|
43 |
+
" if st.button(\"Convert\"):\n",
|
44 |
+
" if image_file is not None:\n",
|
45 |
+
" img = Image.open(image_file)\n",
|
46 |
+
" img = np.array(img)\n",
|
47 |
+
" \n",
|
48 |
+
" st.subheader('Uploaded Image')\n",
|
49 |
+
" st.image(image_file, width=450)\n",
|
50 |
+
" \n",
|
51 |
+
" if source_language == 'English':\n",
|
52 |
+
" with st.spinner('Extracting Text from Image...'):\n",
|
53 |
+
" reader = easyocr.Reader(['en'])\n",
|
54 |
+
" detected_text = reader.readtext(img)\n",
|
55 |
+
" st.subheader('Extracted text:')\n",
|
56 |
+
" text = display_text(detected_text)\n",
|
57 |
+
" st.write(text)\n",
|
58 |
+
" \n",
|
59 |
+
" elif source_language == 'Nepali':\n",
|
60 |
+
" with st.spinner('Extracting Text from Image...'):\n",
|
61 |
+
" reader = easyocr.Reader(['ne'])\n",
|
62 |
+
" detected_text = reader.readtext(img)\n",
|
63 |
+
" st.subheader('Extracted text:')\n",
|
64 |
+
" text = display_text(detected_text)\n",
|
65 |
+
" st.write(text)\n",
|
66 |
+
" \n",
|
67 |
+
" st.subheader('Generated Audio')\n",
|
68 |
+
" with st.spinner('Generating Audio...'):\n",
|
69 |
+
" tts = gTTS(text=text, lang='en' if source_language == 'English' else 'ne')\n",
|
70 |
+
" tts.save('output.mp3')\n",
|
71 |
+
" st.audio('output.mp3', format='audio/mp3')\n",
|
72 |
+
" \n",
|
73 |
+
" else:\n",
|
74 |
+
" st.subheader('Image not found! Please Upload an Image.')\n",
|
75 |
+
"\n"
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"execution_count": 5,
|
81 |
+
"id": "3eb52bd8-2b5b-426e-b23c-b6263d6448c2",
|
82 |
+
"metadata": {},
|
83 |
+
"outputs": [
|
84 |
+
{
|
85 |
+
"name": "stdout",
|
86 |
+
"output_type": "stream",
|
87 |
+
"text": [
|
88 |
+
"Overwriting main.py\n"
|
89 |
+
]
|
90 |
+
}
|
91 |
+
],
|
92 |
+
"source": [
|
93 |
+
"%%writefile main.py\n",
|
94 |
+
"import streamlit as st\n",
|
95 |
+
"\n",
|
96 |
+
"from streamlit_option_menu import option_menu\n",
|
97 |
+
"\n",
|
98 |
+
"\n",
|
99 |
+
"import test, account\n",
|
100 |
+
"st.set_page_config(\n",
|
101 |
+
" page_title=\"Pondering\",\n",
|
102 |
+
")\n",
|
103 |
+
"\n",
|
104 |
+
"\n",
|
105 |
+
"\n",
|
106 |
+
"class MultiApp:\n",
|
107 |
+
"\n",
|
108 |
+
" def __init__(self):\n",
|
109 |
+
" self.apps = []\n",
|
110 |
+
"\n",
|
111 |
+
" def add_app(self, title, func):\n",
|
112 |
+
"\n",
|
113 |
+
" self.apps.append({\n",
|
114 |
+
" \"title\": title,\n",
|
115 |
+
" \"function\": func\n",
|
116 |
+
" })\n",
|
117 |
+
"\n",
|
118 |
+
" def run():\n",
|
119 |
+
" # app = st.sidebar(\n",
|
120 |
+
" with st.sidebar: \n",
|
121 |
+
" app = option_menu(\n",
|
122 |
+
" menu_title='Pondering ',\n",
|
123 |
+
" options=['Home','Account','Trending','Your Posts','about','a'],\n",
|
124 |
+
" icons=['house-fill','person-circle','trophy-fill','chat-fill','info-circle-fill'],\n",
|
125 |
+
" menu_icon='chat-text-fill',\n",
|
126 |
+
" default_index=1,\n",
|
127 |
+
" styles={\n",
|
128 |
+
" \"container\": {\"padding\": \"5!important\",\"background-color\":'black'},\n",
|
129 |
+
" \"icon\": {\"color\": \"white\", \"font-size\": \"23px\"}, \n",
|
130 |
+
" \"nav-link\": {\"color\":\"white\",\"font-size\": \"20px\", \"text-align\": \"left\", \"margin\":\"0px\", \"--hover-color\": \"blue\"},\n",
|
131 |
+
" \"nav-link-selected\": {\"background-color\": \"#02ab21\"},}\n",
|
132 |
+
" \n",
|
133 |
+
" )\n",
|
134 |
+
"\n",
|
135 |
+
" \n",
|
136 |
+
" if app == \"Test\":\n",
|
137 |
+
" home.app()\n",
|
138 |
+
" if app == \"Account\":\n",
|
139 |
+
" account.app() \n",
|
140 |
+
" \n",
|
141 |
+
" \n",
|
142 |
+
" \n",
|
143 |
+
" run() \n",
|
144 |
+
" \n"
|
145 |
+
]
|
146 |
+
},
|
147 |
+
{
|
148 |
+
"cell_type": "code",
|
149 |
+
"execution_count": 3,
|
150 |
+
"id": "535ccb9d-37f1-4a8f-ae9d-a67f0453f716",
|
151 |
+
"metadata": {},
|
152 |
+
"outputs": [
|
153 |
+
{
|
154 |
+
"name": "stdout",
|
155 |
+
"output_type": "stream",
|
156 |
+
"text": [
|
157 |
+
"Writing account.py\n"
|
158 |
+
]
|
159 |
+
}
|
160 |
+
],
|
161 |
+
"source": [
|
162 |
+
"%%writefile account.py\n",
|
163 |
+
"\n",
|
164 |
+
"import streamlit as st\n",
|
165 |
+
"import firebase_admin\n",
|
166 |
+
"from firebase_admin import firestore\n",
|
167 |
+
"from firebase_admin import credentials\n",
|
168 |
+
"from firebase_admin import auth\n",
|
169 |
+
"import json\n",
|
170 |
+
"import requests\n",
|
171 |
+
"\n",
|
172 |
+
"\n",
|
173 |
+
"cred = credentials.Certificate(\"fyp-streamlit-29af1-170f7ef6aa70.json\")\n",
|
174 |
+
"# \n",
|
175 |
+
"firebase_admin.initialize_app(cred)\n",
|
176 |
+
"def app():\n",
|
177 |
+
"# Usernm = []\n",
|
178 |
+
" st.title('Welcome to :violet[Pondering] :sunglasses:')\n",
|
179 |
+
"\n",
|
180 |
+
" if 'username' not in st.session_state:\n",
|
181 |
+
" st.session_state.username = ''\n",
|
182 |
+
" if 'useremail' not in st.session_state:\n",
|
183 |
+
" st.session_state.useremail = ''\n",
|
184 |
+
"\n",
|
185 |
+
"\n",
|
186 |
+
" def sign_up_with_email_and_password(email, password, username=None, return_secure_token=True):\n",
|
187 |
+
" try:\n",
|
188 |
+
" rest_api_url = \"https://identitytoolkit.googleapis.com/v1/accounts:signUp\"\n",
|
189 |
+
" payload = {\n",
|
190 |
+
" \"email\": email,\n",
|
191 |
+
" \"password\": password,\n",
|
192 |
+
" \"returnSecureToken\": return_secure_token\n",
|
193 |
+
" }\n",
|
194 |
+
" if username:\n",
|
195 |
+
" payload[\"displayName\"] = username \n",
|
196 |
+
" payload = json.dumps(payload)\n",
|
197 |
+
" r = requests.post(rest_api_url, params={\"key\": \"AIzaSyA8NET0ZaN6zMe04eOiapmXFF0XKSoFswo\"}, data=payload)\n",
|
198 |
+
" try:\n",
|
199 |
+
" return r.json()['email']\n",
|
200 |
+
" except:\n",
|
201 |
+
" st.warning(r.json())\n",
|
202 |
+
" except Exception as e:\n",
|
203 |
+
" st.warning(f'Signup failed: {e}')\n",
|
204 |
+
"\n",
|
205 |
+
" def sign_in_with_email_and_password(email=None, password=None, return_secure_token=True):\n",
|
206 |
+
" rest_api_url = \"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword\"\n",
|
207 |
+
"\n",
|
208 |
+
" try:\n",
|
209 |
+
" payload = {\n",
|
210 |
+
" \"returnSecureToken\": return_secure_token\n",
|
211 |
+
" }\n",
|
212 |
+
" if email:\n",
|
213 |
+
" payload[\"email\"] = email\n",
|
214 |
+
" if password:\n",
|
215 |
+
" payload[\"password\"] = password\n",
|
216 |
+
" payload = json.dumps(payload)\n",
|
217 |
+
" print('payload sigin',payload)\n",
|
218 |
+
" r = requests.post(rest_api_url, params={\"key\": \"AIzaSyA8NET0ZaN6zMe04eOiapmXFF0XKSoFswo\"}, data=payload)\n",
|
219 |
+
" try:\n",
|
220 |
+
" data = r.json()\n",
|
221 |
+
" user_info = {\n",
|
222 |
+
" 'email': data['email'],\n",
|
223 |
+
" 'username': data.get('displayName') # Retrieve username if available\n",
|
224 |
+
" }\n",
|
225 |
+
" return user_info\n",
|
226 |
+
" except:\n",
|
227 |
+
" st.warning(data)\n",
|
228 |
+
" except Exception as e:\n",
|
229 |
+
" st.warning(f'Signin failed: {e}')\n",
|
230 |
+
"\n",
|
231 |
+
" def reset_password(email):\n",
|
232 |
+
" try:\n",
|
233 |
+
" rest_api_url = \"https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode\"\n",
|
234 |
+
" payload = {\n",
|
235 |
+
" \"email\": email,\n",
|
236 |
+
" \"requestType\": \"PASSWORD_RESET\"\n",
|
237 |
+
" }\n",
|
238 |
+
" payload = json.dumps(payload)\n",
|
239 |
+
" r = requests.post(rest_api_url, params={\"key\": \"AIzaSyA8NET0ZaN6zMe04eOiapmXFF0XKSoFswo\"}, data=payload)\n",
|
240 |
+
" if r.status_code == 200:\n",
|
241 |
+
" return True, \"Reset email Sent\"\n",
|
242 |
+
" else:\n",
|
243 |
+
" # Handle error response\n",
|
244 |
+
" error_message = r.json().get('error', {}).get('message')\n",
|
245 |
+
" return False, error_message\n",
|
246 |
+
" except Exception as e:\n",
|
247 |
+
" return False, str(e)\n",
|
248 |
+
"\n",
|
249 |
+
" # Example usage\n",
|
250 |
+
" # email = \"example@example.com\"\n",
|
251 |
+
" \n",
|
252 |
+
"\n",
|
253 |
+
" def f(): \n",
|
254 |
+
" try:\n",
|
255 |
+
" # user = auth.get_user_by_email(email)\n",
|
256 |
+
" # print(user.uid)\n",
|
257 |
+
" # st.session_state.username = user.uid\n",
|
258 |
+
" # st.session_state.useremail = user.email\n",
|
259 |
+
"\n",
|
260 |
+
" userinfo = sign_in_with_email_and_password(st.session_state.email_input,st.session_state.password_input)\n",
|
261 |
+
" st.session_state.username = userinfo['username']\n",
|
262 |
+
" st.session_state.useremail = userinfo['email']\n",
|
263 |
+
"\n",
|
264 |
+
" \n",
|
265 |
+
" global Usernm\n",
|
266 |
+
" Usernm=(userinfo['username'])\n",
|
267 |
+
" \n",
|
268 |
+
" st.session_state.signedout = True\n",
|
269 |
+
" st.session_state.signout = True \n",
|
270 |
+
" \n",
|
271 |
+
" \n",
|
272 |
+
" except: \n",
|
273 |
+
" st.warning('Login Failed')\n",
|
274 |
+
"\n",
|
275 |
+
" def t():\n",
|
276 |
+
" st.session_state.signout = False\n",
|
277 |
+
" st.session_state.signedout = False \n",
|
278 |
+
" st.session_state.username = ''\n",
|
279 |
+
"\n",
|
280 |
+
"\n",
|
281 |
+
" def forget():\n",
|
282 |
+
" email = st.text_input('Email')\n",
|
283 |
+
" if st.button('Send Reset Link'):\n",
|
284 |
+
" print(email)\n",
|
285 |
+
" success, message = reset_password(email)\n",
|
286 |
+
" if success:\n",
|
287 |
+
" st.success(\"Password reset email sent successfully.\")\n",
|
288 |
+
" else:\n",
|
289 |
+
" st.warning(f\"Password reset failed: {message}\") \n",
|
290 |
+
" \n",
|
291 |
+
" \n",
|
292 |
+
" \n",
|
293 |
+
" if \"signedout\" not in st.session_state:\n",
|
294 |
+
" st.session_state[\"signedout\"] = False\n",
|
295 |
+
" if 'signout' not in st.session_state:\n",
|
296 |
+
" st.session_state['signout'] = False \n",
|
297 |
+
" \n",
|
298 |
+
"\n",
|
299 |
+
" \n",
|
300 |
+
" \n",
|
301 |
+
" if not st.session_state[\"signedout\"]: # only show if the state is False, hence the button has never been clicked\n",
|
302 |
+
" choice = st.selectbox('Login/Signup',['Login','Sign up'])\n",
|
303 |
+
" email = st.text_input('Email Address')\n",
|
304 |
+
" password = st.text_input('Password',type='password')\n",
|
305 |
+
" st.session_state.email_input = email\n",
|
306 |
+
" st.session_state.password_input = password\n",
|
307 |
+
"\n",
|
308 |
+
" \n",
|
309 |
+
"\n",
|
310 |
+
" \n",
|
311 |
+
" if choice == 'Sign up':\n",
|
312 |
+
" username = st.text_input(\"Enter your unique username\")\n",
|
313 |
+
" \n",
|
314 |
+
" if st.button('Create my account'):\n",
|
315 |
+
" # user = auth.create_user(email = email, password = password,uid=username)\n",
|
316 |
+
" user = sign_up_with_email_and_password(email=email,password=password,username=username)\n",
|
317 |
+
" \n",
|
318 |
+
" st.success('Account created successfully!')\n",
|
319 |
+
" st.markdown('Please Login using your email and password')\n",
|
320 |
+
" st.balloons()\n",
|
321 |
+
" else:\n",
|
322 |
+
" # st.button('Login', on_click=f) \n",
|
323 |
+
" st.button('Login', on_click=f)\n",
|
324 |
+
" # if st.button('Forget'):\n",
|
325 |
+
" forget()\n",
|
326 |
+
" # st.button('Forget',on_click=forget)\n",
|
327 |
+
"\n",
|
328 |
+
" \n",
|
329 |
+
" \n",
|
330 |
+
" if st.session_state.signout:\n",
|
331 |
+
" st.text('Name '+st.session_state.username)\n",
|
332 |
+
" st.text('Email id: '+st.session_state.useremail)\n",
|
333 |
+
" st.button('Sign out', on_click=t) \n",
|
334 |
+
" \n",
|
335 |
+
" \n",
|
336 |
+
" \n",
|
337 |
+
"\n",
|
338 |
+
" \n",
|
339 |
+
" def ap():\n",
|
340 |
+
" st.write('Posts')\n"
|
341 |
+
]
|
342 |
+
},
|
343 |
+
{
|
344 |
+
"cell_type": "code",
|
345 |
+
"execution_count": 7,
|
346 |
+
"id": "b6eda571-3f8d-47db-b5f7-74fe747837eb",
|
347 |
+
"metadata": {},
|
348 |
+
"outputs": [
|
349 |
+
{
|
350 |
+
"name": "stdout",
|
351 |
+
"output_type": "stream",
|
352 |
+
"text": [
|
353 |
+
"Writing history.py\n"
|
354 |
+
]
|
355 |
+
}
|
356 |
+
],
|
357 |
+
"source": [
|
358 |
+
"%%writefile history.py\n",
|
359 |
+
"import streamlit as st\n",
|
360 |
+
"from firebase_admin import firestore\n",
|
361 |
+
"\n",
|
362 |
+
" \n",
|
363 |
+
"def app():\n",
|
364 |
+
" db=firestore.client()\n",
|
365 |
+
"\n",
|
366 |
+
"\n",
|
367 |
+
" try:\n",
|
368 |
+
" st.title('Posted by: '+st.session_state['username'] )\n",
|
369 |
+
"\n",
|
370 |
+
" \n",
|
371 |
+
" result = db.collection('Posts').document(st.session_state['username']).get()\n",
|
372 |
+
" r=result.to_dict()\n",
|
373 |
+
" content = r['Content']\n",
|
374 |
+
" \n",
|
375 |
+
" \n",
|
376 |
+
" def delete_post(k):\n",
|
377 |
+
" c=int(k)\n",
|
378 |
+
" h=content[c]\n",
|
379 |
+
" try:\n",
|
380 |
+
" db.collection('Posts').document(st.session_state['username']).update({\"Content\": firestore.ArrayRemove([h])})\n",
|
381 |
+
" st.warning('Post deleted')\n",
|
382 |
+
" except:\n",
|
383 |
+
" st.write('Something went wrong..')\n",
|
384 |
+
" \n",
|
385 |
+
" for c in range(len(content)-1,-1,-1):\n",
|
386 |
+
" st.text_area(label='',value=content[c])\n",
|
387 |
+
" st.button('Delete Post', on_click=delete_post, args=([c] ), key=c) \n",
|
388 |
+
"\n",
|
389 |
+
" \n",
|
390 |
+
" except:\n",
|
391 |
+
" if st.session_state.username=='':\n",
|
392 |
+
" st.text('Please Login first') "
|
393 |
+
]
|
394 |
+
},
|
395 |
+
{
|
396 |
+
"cell_type": "code",
|
397 |
+
"execution_count": null,
|
398 |
+
"id": "a9b5e98c-0e05-4030-9f5d-9bf5812f35ca",
|
399 |
+
"metadata": {},
|
400 |
+
"outputs": [],
|
401 |
+
"source": []
|
402 |
+
}
|
403 |
+
],
|
404 |
+
"metadata": {
|
405 |
+
"kernelspec": {
|
406 |
+
"display_name": "Python 3 (ipykernel)",
|
407 |
+
"language": "python",
|
408 |
+
"name": "python3"
|
409 |
+
},
|
410 |
+
"language_info": {
|
411 |
+
"codemirror_mode": {
|
412 |
+
"name": "ipython",
|
413 |
+
"version": 3
|
414 |
+
},
|
415 |
+
"file_extension": ".py",
|
416 |
+
"mimetype": "text/x-python",
|
417 |
+
"name": "python",
|
418 |
+
"nbconvert_exporter": "python",
|
419 |
+
"pygments_lexer": "ipython3",
|
420 |
+
"version": "3.9.7"
|
421 |
+
}
|
422 |
+
},
|
423 |
+
"nbformat": 4,
|
424 |
+
"nbformat_minor": 5
|
425 |
+
}
|
account.py
ADDED
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import firebase_admin
|
4 |
+
from firebase_admin import firestore
|
5 |
+
from firebase_admin import credentials
|
6 |
+
from firebase_admin import auth
|
7 |
+
import json
|
8 |
+
import requests
|
9 |
+
|
10 |
+
|
11 |
+
cred = credentials.Certificate("fyp-streamlit-29af1-170f7ef6aa70.json")
|
12 |
+
|
13 |
+
firebase_admin.initialize_app(cred)
|
14 |
+
def app():
|
15 |
+
# Usernm = []
|
16 |
+
st.title('Welcome to LearnLite :brain:')
|
17 |
+
|
18 |
+
if 'username' not in st.session_state:
|
19 |
+
st.session_state.username = ''
|
20 |
+
if 'useremail' not in st.session_state:
|
21 |
+
st.session_state.useremail = ''
|
22 |
+
|
23 |
+
|
24 |
+
def sign_up_with_email_and_password(email, password, username=None, return_secure_token=True):
|
25 |
+
try:
|
26 |
+
rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:signUp"
|
27 |
+
payload = {
|
28 |
+
"email": email,
|
29 |
+
"password": password,
|
30 |
+
"returnSecureToken": return_secure_token
|
31 |
+
}
|
32 |
+
if username:
|
33 |
+
payload["displayName"] = username
|
34 |
+
payload = json.dumps(payload)
|
35 |
+
r = requests.post(rest_api_url, params={"key": "AIzaSyA8NET0ZaN6zMe04eOiapmXFF0XKSoFswo"}, data=payload)
|
36 |
+
try:
|
37 |
+
return r.json()['email']
|
38 |
+
except:
|
39 |
+
st.warning(r.json())
|
40 |
+
except Exception as e:
|
41 |
+
st.warning(f'Signup failed: {e}')
|
42 |
+
|
43 |
+
def sign_in_with_email_and_password(email=None, password=None, return_secure_token=True):
|
44 |
+
rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword"
|
45 |
+
|
46 |
+
try:
|
47 |
+
payload = {
|
48 |
+
"returnSecureToken": return_secure_token
|
49 |
+
}
|
50 |
+
if email:
|
51 |
+
payload["email"] = email
|
52 |
+
if password:
|
53 |
+
payload["password"] = password
|
54 |
+
payload = json.dumps(payload)
|
55 |
+
print('payload sigin',payload)
|
56 |
+
r = requests.post(rest_api_url, params={"key": "AIzaSyA8NET0ZaN6zMe04eOiapmXFF0XKSoFswo"}, data=payload)
|
57 |
+
try:
|
58 |
+
data = r.json()
|
59 |
+
user_info = {
|
60 |
+
'email': data['email'],
|
61 |
+
'username': data.get('displayName') # Retrieve username if available
|
62 |
+
}
|
63 |
+
return user_info
|
64 |
+
except:
|
65 |
+
st.warning(data)
|
66 |
+
except Exception as e:
|
67 |
+
st.warning(f'Signin failed: {e}')
|
68 |
+
|
69 |
+
def reset_password(email):
|
70 |
+
try:
|
71 |
+
rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode"
|
72 |
+
payload = {
|
73 |
+
"email": email,
|
74 |
+
"requestType": "PASSWORD_RESET"
|
75 |
+
}
|
76 |
+
payload = json.dumps(payload)
|
77 |
+
r = requests.post(rest_api_url, params={"key": "AIzaSyA8NET0ZaN6zMe04eOiapmXFF0XKSoFswo"}, data=payload)
|
78 |
+
if r.status_code == 200:
|
79 |
+
return True, "Reset email Sent"
|
80 |
+
else:
|
81 |
+
# Handle error response
|
82 |
+
error_message = r.json().get('error', {}).get('message')
|
83 |
+
return False, error_message
|
84 |
+
except Exception as e:
|
85 |
+
return False, str(e)
|
86 |
+
|
87 |
+
# Example usage
|
88 |
+
# email = "example@example.com"
|
89 |
+
|
90 |
+
|
91 |
+
def f():
|
92 |
+
try:
|
93 |
+
# user = auth.get_user_by_email(email)
|
94 |
+
# print(user.uid)
|
95 |
+
# st.session_state.username = user.uid
|
96 |
+
# st.session_state.useremail = user.email
|
97 |
+
|
98 |
+
userinfo = sign_in_with_email_and_password(st.session_state.email_input,st.session_state.password_input)
|
99 |
+
st.session_state.username = userinfo['username']
|
100 |
+
st.session_state.useremail = userinfo['email']
|
101 |
+
|
102 |
+
|
103 |
+
global Usernm
|
104 |
+
Usernm=(userinfo['username'])
|
105 |
+
|
106 |
+
st.session_state.signedout = True
|
107 |
+
st.session_state.signout = True
|
108 |
+
|
109 |
+
|
110 |
+
except:
|
111 |
+
st.warning('Login Failed')
|
112 |
+
|
113 |
+
def t():
|
114 |
+
st.session_state.signout = False
|
115 |
+
st.session_state.signedout = False
|
116 |
+
st.session_state.username = ''
|
117 |
+
|
118 |
+
|
119 |
+
def forget():
|
120 |
+
email = st.text_input('Email')
|
121 |
+
if st.button('Send Reset Link'):
|
122 |
+
print(email)
|
123 |
+
success, message = reset_password(email)
|
124 |
+
if success:
|
125 |
+
st.success("Password reset email sent successfully.")
|
126 |
+
else:
|
127 |
+
st.warning(f"Password reset failed: {message}")
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
if "signedout" not in st.session_state:
|
132 |
+
st.session_state["signedout"] = False
|
133 |
+
if 'signout' not in st.session_state:
|
134 |
+
st.session_state['signout'] = False
|
135 |
+
|
136 |
+
|
137 |
+
|
138 |
+
|
139 |
+
if not st.session_state["signedout"]: # only show if the state is False, hence the button has never been clicked
|
140 |
+
choice = st.selectbox('Login/Signup',['Login','Sign up'])
|
141 |
+
email = st.text_input('Email Address')
|
142 |
+
password = st.text_input('Password',type='password')
|
143 |
+
st.session_state.email_input = email
|
144 |
+
st.session_state.password_input = password
|
145 |
+
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
if choice == 'Sign up':
|
150 |
+
username = st.text_input("Enter your unique username")
|
151 |
+
|
152 |
+
if st.button('Create my account'):
|
153 |
+
# user = auth.create_user(email = email, password = password,uid=username)
|
154 |
+
user = sign_up_with_email_and_password(email=email,password=password,username=username)
|
155 |
+
|
156 |
+
st.success('Account created successfully!')
|
157 |
+
st.markdown('Please Login using your email and password')
|
158 |
+
st.balloons()
|
159 |
+
else:
|
160 |
+
# st.button('Login', on_click=f)
|
161 |
+
st.button('Login', on_click=f)
|
162 |
+
# if st.button('Forget'):
|
163 |
+
forget()
|
164 |
+
# st.button('Forget',on_click=forget)
|
165 |
+
|
166 |
+
|
167 |
+
|
168 |
+
if st.session_state.signout:
|
169 |
+
st.text('Name '+st.session_state.username)
|
170 |
+
st.text('Email id: '+st.session_state.useremail)
|
171 |
+
st.button('Sign out', on_click=t)
|
172 |
+
|
173 |
+
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
def ap():
|
178 |
+
st.write('Posts')
|
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
from streamlit_option_menu import option_menu
|
4 |
+
|
5 |
+
|
6 |
+
import ocr, account, history
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="Learnlite",
|
9 |
+
)
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
class MultiApp:
|
14 |
+
|
15 |
+
def __init__(self):
|
16 |
+
self.apps = []
|
17 |
+
|
18 |
+
def add_app(self, title, func):
|
19 |
+
|
20 |
+
self.apps.append({
|
21 |
+
"title": title,
|
22 |
+
"function": func
|
23 |
+
})
|
24 |
+
|
25 |
+
def run():
|
26 |
+
# app = st.sidebar(
|
27 |
+
with st.sidebar:
|
28 |
+
app = option_menu(
|
29 |
+
menu_title='LearnLite ',
|
30 |
+
options=['Account','OCR & Text Summarization','History'],
|
31 |
+
icons=['person-circle','chat-text-fill','chat-fill'],
|
32 |
+
menu_icon='chat-text-fill',
|
33 |
+
default_index=0,
|
34 |
+
styles={
|
35 |
+
}
|
36 |
+
|
37 |
+
)
|
38 |
+
|
39 |
+
if app == "Account":
|
40 |
+
account.app()
|
41 |
+
|
42 |
+
if app == "OCR & Text Summarization":
|
43 |
+
ocr.app()
|
44 |
+
|
45 |
+
if app=="History":
|
46 |
+
history.app()
|
47 |
+
|
48 |
+
|
49 |
+
run()
|
50 |
+
|
fyp-streamlit-29af1-170f7ef6aa70.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"type": "service_account",
|
3 |
+
"project_id": "fyp-streamlit-29af1",
|
4 |
+
"private_key_id": "170f7ef6aa7024f30276233e863903a7487149ef",
|
5 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCg4Qji9TnbKlvY\nbJIx+fSFMmcwnFobaOJgJbR5r2/f3yQw0/h+afFfQJpWK76BzyUdbDsduzFhFDT+\nEOxcQ5dv4RfnMGfQKW3FVvameoHiozIYA5/gXgke6QIbarmZEs/zBESgSf5qZIx4\n03YOQXdH2XQ/9amz3xVXINC9hH0GeF0Su00ZeGq8OWay3jkn7zCnQNkacnPt1Xgd\n7t3YdqE0MzuGkoAxbzdVJgvRKtwHScVd4zv8htVC/wuNXjgeMLzuR3RjMLZUMqM2\nmVtkSXNGP7sL73QA2Kq4StI8CHFYrkVCGnoYyMP+xpPovyJO1JG043WBNqsVW6pv\nHcbPNItrAgMBAAECggEAG2cZV2dDypQFfvDHWiHkR4fVUc9f/ck4vf3OJuf+G1jr\n1jFU41tyVLIxV7otwfihrshPrXnEKHADsr75M6IDunfR1XxGQou7+eAdNTAqzYrU\nroh3hAUgcpZn2HmuUWeF6jPw6IiSAFdOIgqK0lTOV3uvtHFIza2S+ftJ8u4NSbuT\ncwUfToUe9Gw8dJrdvPQf+7MoekEdolf81rfhhzoRlT0VMDIhZKqzdl+LkHQ5gTvP\npB7d9iqibbW7EHWTvL4UIGKgdaq3DvNDOog61an8leFIDZ8bUIVGU2D05w0RvNMx\nV2Yg+PLI/KQTfJKKI27aaAJ0z9+nf2F3lgbenuvnMQKBgQDapyJzkkncZaWhuPGy\nj9zfo0nHYblyEVowJajw9KjpKF9bLLARW+UXvybiI+YI4vw6iksHxt9UekZjyyEc\nK/65AMlT3+CG/YZjXGyIV+QDElYRMzpxZuxhFXfdzRcDnmvl9WJVYrXaTWFiWVj4\nwYG48Tz1nB2q5QL10t6tLFZp0wKBgQC8W6skht+5iAQkuYs8jD3GmLRmMmn7qKlw\n3a1qd8yCAl0wVa4zguj+bFm0DdI64TeHkla6ht51p1msInNRaxQ0oGXeF69ivScS\n7m/wy3ov1UttjuHkLYdRTbasDpRlCdaJtkTISjP7zNgy/XQW1uR/Tl/qjFwMS++D\nVtFxbZsBCQKBgArR2G+VnpgMY5zUFQm7+rcKUn1ORanyV98oDesWgEFBYS6W0IOb\nY4PATsFD+38XFFolO78/xQ9eGZuYkqPdPXaUJmJDIs4rP3otOaWpi7vnLWEegtbX\nu/y13j+YhXoFXdt8QAhvoQYTbC08tJ1infOz5EtXFVS+jl3xuFkjZ4dHAoGBAJIu\nWn44pnzo0L5nuWW+OVtdu/PsgFAtL4NGp4WPgAoOJ6CRfma/3G9IS73vVA8aJuxy\naBjO/us0e+WZvwGbJs1JBoX8v8LTpfWNUxb/884XrThTwYBCzVgOBg02K3jWz8eE\ny5Dd9cYfzlKUXXo0QJWEj/AKCo4IiNPXlyh+teNxAoGBAK1M0BP4Qi5DpaftorhP\n0IiCshwh5iN+QmlDvLEnBT2Z+sIYEEjEV91/oVDDnG4XR6RRCQybS4wO6e+aosCo\ntnKA/ebpzFEkf5ywqgSA3Nhng6ZykzEIyPhEwZ0w297nyrHVZCMLjntPocImhlEH\njP6CoLgIzKTwuUiUQihEncAB\n-----END PRIVATE KEY-----\n",
|
6 |
+
"client_email": "firebase-adminsdk-7g89c@fyp-streamlit-29af1.iam.gserviceaccount.com",
|
7 |
+
"client_id": "116868549596616830623",
|
8 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
9 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
10 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
11 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-7g89c%40fyp-streamlit-29af1.iam.gserviceaccount.com",
|
12 |
+
"universe_domain": "googleapis.com"
|
13 |
+
}
|
history.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from firebase_admin import firestore
|
3 |
+
|
4 |
+
|
5 |
+
def app():
|
6 |
+
db=firestore.client()
|
7 |
+
|
8 |
+
|
9 |
+
try:
|
10 |
+
result = db.collection('History').document(st.session_state['useremail']).get()
|
11 |
+
r=result.to_dict()
|
12 |
+
summary = r['Summary']
|
13 |
+
ocr = r['OCR']
|
14 |
+
if st.session_state.username=='':
|
15 |
+
st.title('History of: '+st.session_state['useremail'] )
|
16 |
+
else:
|
17 |
+
st.title('History of: '+st.session_state['username'] )
|
18 |
+
|
19 |
+
|
20 |
+
def delete_post(k):
|
21 |
+
h=ocr[c]
|
22 |
+
try:
|
23 |
+
db.collection('History').document(st.session_state['useremail']).update({"OCR": firestore.ArrayRemove([h])})
|
24 |
+
db.collection('History').document(st.session_state['useremail']).update({"Summary": firestore.ArrayRemove([h])})
|
25 |
+
st.warning('History deleted')
|
26 |
+
except:
|
27 |
+
st.write('Something went wrong..')
|
28 |
+
|
29 |
+
for c in range(len(ocr)):
|
30 |
+
st.text_area(label='OCR',value=ocr[c])
|
31 |
+
st.text_area(label='Summary',value=summary[c])
|
32 |
+
st.button('Delete Post', on_click=delete_post, args=([c] ), key=c)
|
33 |
+
|
34 |
+
|
35 |
+
except:
|
36 |
+
if st.session_state.username=='' and st.session_state.useremail=='':
|
37 |
+
st.subheader('Please Login first...')
|
ocr.py
ADDED
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import streamlit as st
|
2 |
+
# import easyocr
|
3 |
+
# from PIL import Image
|
4 |
+
# import numpy as np
|
5 |
+
# from gtts import gTTS
|
6 |
+
# from firebase_admin import firestore
|
7 |
+
|
8 |
+
# def app():
|
9 |
+
# try:
|
10 |
+
# if 'db' not in st.session_state:
|
11 |
+
# st.session_state.db = ''
|
12 |
+
|
13 |
+
# db=firestore.client()
|
14 |
+
# st.session_state.db=db
|
15 |
+
# info = db.collection('History').document(st.session_state.useremail).get()
|
16 |
+
# def display_text(bounds):
|
17 |
+
# text = []
|
18 |
+
# for x in bounds:
|
19 |
+
# t = x[1]
|
20 |
+
# text.append(t)
|
21 |
+
# text = ' '.join(text)
|
22 |
+
# return text
|
23 |
+
|
24 |
+
# st.set_option('deprecation.showfileUploaderEncoding', False)
|
25 |
+
# st.title('OCR - Optical Character Recognition')
|
26 |
+
# st.subheader('Extract text from images')
|
27 |
+
# st.text('Note: OCR only works for English text at the moment.')
|
28 |
+
|
29 |
+
# image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg', 'JPG'])
|
30 |
+
|
31 |
+
# if st.button("Convert"):
|
32 |
+
# if image_file is not None:
|
33 |
+
# img = Image.open(image_file)
|
34 |
+
# img = np.array(img)
|
35 |
+
|
36 |
+
# st.subheader('Uploaded Image')
|
37 |
+
# st.image(image_file, width=450)
|
38 |
+
|
39 |
+
# with st.spinner('Extracting Text from Image...'):
|
40 |
+
# reader = easyocr.Reader(['en'])
|
41 |
+
# detected_text = reader.readtext(img)
|
42 |
+
# st.subheader('Extracted text:')
|
43 |
+
# text = display_text(detected_text)
|
44 |
+
# st.write(text)
|
45 |
+
|
46 |
+
# st.subheader('Generated Audio')
|
47 |
+
# with st.spinner('Generating Audio...'):
|
48 |
+
# tts = gTTS(text=text, lang='en')
|
49 |
+
# tts.save('output.mp3')
|
50 |
+
# st.audio('output.mp3', format='audio/mp3')
|
51 |
+
# with st.spinner('Saving Result...'):
|
52 |
+
# if info.exists:
|
53 |
+
# info = info.to_dict()
|
54 |
+
# if 'OCR' in info.keys():
|
55 |
+
|
56 |
+
# pos=db.collection('History').document(st.session_state.useremail)
|
57 |
+
# pos.update({u'OCR': firestore.ArrayUnion([u'{}'.format(text)])})
|
58 |
+
# pos.update({u'Summary': firestore.ArrayUnion([u'{}'.format(text)])})
|
59 |
+
# # st.write('Post uploaded!!')
|
60 |
+
# else:
|
61 |
+
|
62 |
+
# data={"OCR":[text],"Summary":[text],'email':st.session_state.useremail}
|
63 |
+
# db.collection('History').document(st.session_state.useremail).set(data)
|
64 |
+
# else:
|
65 |
+
# data={"OCR":[text],"Summary":[text],'email':st.session_state.useremail}
|
66 |
+
# db.collection('History').document(st.session_state.useremail).set(data)
|
67 |
+
# st.success('Post uploaded!!')
|
68 |
+
|
69 |
+
|
70 |
+
# else:
|
71 |
+
# st.subheader('Image not found! Please Upload an Image.')
|
72 |
+
# except:
|
73 |
+
# if st.session_state.username=='' and st.session_state.useremail=='':
|
74 |
+
# st.subheader('Please Login first...')
|
75 |
+
|
76 |
+
|
77 |
+
import streamlit as st
|
78 |
+
import pytesseract
|
79 |
+
from PIL import Image
|
80 |
+
import numpy as np
|
81 |
+
from deskew import determine_skew
|
82 |
+
from gtts import gTTS
|
83 |
+
from typing import Tuple, Union
|
84 |
+
import math
|
85 |
+
import cv2
|
86 |
+
import io
|
87 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
88 |
+
import textwrap
|
89 |
+
|
90 |
+
|
91 |
+
@st.cache_resource
|
92 |
+
def load_model():
|
93 |
+
model_path = "/content/drive/MyDrive/Models/pegasus-news"
|
94 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
95 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
96 |
+
return tokenizer, model
|
97 |
+
|
98 |
+
tokenizer, model = load_model()
|
99 |
+
|
100 |
+
def summarize_text(text, max_length=512):
|
101 |
+
summarizer = pipeline("summarization", model=model, tokenizer=tokenizer, max_length=max_length, min_length=30)
|
102 |
+
paragraphs = textwrap.wrap(text, width=max_length, break_long_words=False)
|
103 |
+
summaries = []
|
104 |
+
|
105 |
+
total_paragraphs = len(paragraphs)
|
106 |
+
progress_bar = st.progress(0)
|
107 |
+
|
108 |
+
for i, paragraph in enumerate(paragraphs):
|
109 |
+
summary = summarizer(paragraph)
|
110 |
+
summaries.append(summary[0]['summary_text'])
|
111 |
+
progress_bar.progress((i + 1) / total_paragraphs)
|
112 |
+
|
113 |
+
final_summary = ' '.join(summaries)
|
114 |
+
return final_summary
|
115 |
+
|
116 |
+
|
117 |
+
pytesseract.pytesseract.tesseract_cmd = r'./Tesseract-OCR/tesseract.exe'
|
118 |
+
# pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
|
119 |
+
def rotate(
|
120 |
+
image: np.ndarray, angle: float, background: Union[int, Tuple[int, int, int]]
|
121 |
+
) -> np.ndarray:
|
122 |
+
old_width, old_height = image.shape[:2]
|
123 |
+
angle_radian = math.radians(angle)
|
124 |
+
width = abs(np.sin(angle_radian) * old_height) + abs(np.cos(angle_radian) * old_width)
|
125 |
+
height = abs(np.sin(angle_radian) * old_width) + abs(np.cos(angle_radian) * old_height)
|
126 |
+
|
127 |
+
image_center = tuple(np.array(image.shape[1::-1]) / 2)
|
128 |
+
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
|
129 |
+
rot_mat[1, 2] += (width - old_width) / 2
|
130 |
+
rot_mat[0, 2] += (height - old_height) / 2
|
131 |
+
return cv2.warpAffine(image, rot_mat, (int(round(height)), int(round(width))), borderValue=background)
|
132 |
+
|
133 |
+
|
134 |
+
def pre_process_image(img):
|
135 |
+
"""This function will pre-process a image with: cv2 & deskew
|
136 |
+
so it can be process by tesseract"""
|
137 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #change color format from BGR to RGB
|
138 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #format image to gray scale
|
139 |
+
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 11) #to remove background
|
140 |
+
return img
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
# st.sidebar.title('Language Selection Menu')
|
145 |
+
# st.sidebar.subheader('Select Source Language')
|
146 |
+
# source_language = st.sidebar.selectbox("Source Language", ['English', 'Nepali'])
|
147 |
+
|
148 |
+
def display_text(bounds):
|
149 |
+
text = []
|
150 |
+
for x in bounds:
|
151 |
+
t = x[1]
|
152 |
+
text.append(t)
|
153 |
+
text = ' '.join(text)
|
154 |
+
return text
|
155 |
+
|
156 |
+
|
157 |
+
def app():
|
158 |
+
try:
|
159 |
+
if 'db' not in st.session_state:
|
160 |
+
st.session_state.db = ''
|
161 |
+
|
162 |
+
db=firestore.client()
|
163 |
+
st.session_state.db=db
|
164 |
+
info = db.collection('History').document(st.session_state.useremail).get()
|
165 |
+
def display_text(bounds):
|
166 |
+
text = []
|
167 |
+
for x in bounds:
|
168 |
+
t = x[1]
|
169 |
+
text.append(t)
|
170 |
+
text = ' '.join(text)
|
171 |
+
return text
|
172 |
+
st.set_option('deprecation.showfileUploaderEncoding', False)
|
173 |
+
st.title('OCR - Optical Character Recognition')
|
174 |
+
st.subheader('Extract text from images')
|
175 |
+
st.text('Note: OCR only works for English text at the moment.')
|
176 |
+
|
177 |
+
image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg', 'JPG'])
|
178 |
+
|
179 |
+
if st.button("Convert"):
|
180 |
+
if image_file is not None:
|
181 |
+
img= Image.open(image_file)
|
182 |
+
image = np.array(img)
|
183 |
+
#image = cv2.imread('imgp')
|
184 |
+
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
185 |
+
angle = determine_skew(grayscale)
|
186 |
+
rotated = rotate(image, angle, (0, 0, 0))
|
187 |
+
cv2.imwrite('output.png', rotated)
|
188 |
+
img = pre_process_image(rotated)
|
189 |
+
|
190 |
+
st.subheader('Uploaded Image')
|
191 |
+
st.image(img, width=450)
|
192 |
+
|
193 |
+
# if source_language == 'English':
|
194 |
+
with st.spinner('Extracting Text from Image...'):
|
195 |
+
detected_text = pytesseract.image_to_string(img, lang='eng')
|
196 |
+
st.subheader('Extracted text:')
|
197 |
+
st.write(detected_text)
|
198 |
+
|
199 |
+
|
200 |
+
# elif source_language == 'Nepali':
|
201 |
+
# with st.spinner('Extracting Text from Image...'):
|
202 |
+
# detected_text = pytesseract.image_to_string(img, lang='nep')
|
203 |
+
# st.subheader('Extracted text:')
|
204 |
+
# st.write(detected_text)
|
205 |
+
|
206 |
+
st.subheader('Generated Audio')
|
207 |
+
with st.spinner('Generating Audio...'):
|
208 |
+
tts = gTTS(text=detected_text, lang='en')
|
209 |
+
tts.save('output.mp3')
|
210 |
+
st.audio('output.mp3', format='audio/mp3')
|
211 |
+
|
212 |
+
|
213 |
+
st.subheader("Text Summary")
|
214 |
+
with st.spinner('Generating Summary...'):
|
215 |
+
summary = summarize_text(detected_text)
|
216 |
+
#st.write(summary[0]['summary_text'])
|
217 |
+
st.write(summary)
|
218 |
+
|
219 |
+
with st.spinner('Saving Result...'):
|
220 |
+
if info.exists:
|
221 |
+
info = info.to_dict()
|
222 |
+
if 'OCR' in info.keys():
|
223 |
+
|
224 |
+
pos=db.collection('History').document(st.session_state.useremail)
|
225 |
+
pos.update({u'OCR': firestore.ArrayUnion([u'{}'.format(detected_text)])})
|
226 |
+
pos.update({u'Summary': firestore.ArrayUnion([u'{}'.format(summary)])})
|
227 |
+
st.write('Post uploaded!!')
|
228 |
+
else:
|
229 |
+
|
230 |
+
data={"OCR":[detected_text],"Summary":[summary],'email':st.session_state.useremail}
|
231 |
+
db.collection('History').document(st.session_state.useremail).set(data)
|
232 |
+
else:
|
233 |
+
data={"OCR":[detected_text],"Summary":[summary],'email':st.session_state.useremail}
|
234 |
+
db.collection('History').document(st.session_state.useremail).set(data)
|
235 |
+
st.success('Post uploaded!!')
|
236 |
+
|
237 |
+
else:
|
238 |
+
st.subheader('Image not found! Please Upload an Image.')
|
239 |
+
|
240 |
+
except:
|
241 |
+
if st.session_state.username=='' and st.session_state.useremail=='':
|
242 |
+
st.subheader('Please Login first...')
|
output.mp3
ADDED
Binary file (220 kB). View file
|
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
tesseract-ocr-all
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pytesseract
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
deskew
|
5 |
+
gTTS
|