paulokewunmi commited on
Commit
dd7fea7
1 Parent(s): da2a66c

Add Translate Feature

Browse files
__pycache__/app.cpython-311.pyc CHANGED
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
 
app.py CHANGED
@@ -8,9 +8,10 @@ from src.document_utils import (
8
  load_history,
9
  load_science,
10
  )
11
- from src.wiki_search import cross_lingual_document_search, document_source
12
  from src.theme import CustomTheme
13
 
 
14
  max_search_results = 3
15
 
16
 
@@ -27,9 +28,13 @@ def study_doc_qa_bot(input_document, history):
27
  history[-1][1] = bot_message
28
  return history
29
 
30
- def my_function(file_object):
31
- pass
32
 
 
 
 
 
33
 
34
  custom_theme = CustomTheme()
35
 
@@ -72,7 +77,7 @@ with gr.Blocks(theme=custom_theme) as demo:
72
  with gr.Column():
73
  user_query = gr.Text(
74
  label="Enter query here",
75
- placeholder="Search through study materials (e.g The history of Lagos)",
76
  )
77
 
78
  num_search_results = gr.Slider(
@@ -94,14 +99,18 @@ with gr.Blocks(theme=custom_theme) as demo:
94
 
95
  with gr.Column():
96
  with gr.Accordion("Click to View Source", open=False):
97
- # gr.Button(
98
- # label="Go to Source",
99
- # value="Get Sources",
100
- # variant="primary",
101
- # ).click(fn=None, _js="window.open('https://google.com', '_blank')")
102
 
 
 
 
 
 
 
103
  translate_res_1 = gr.Textbox(
104
- label=f"Source Url",
105
  )
106
 
107
  with gr.Row():
@@ -110,19 +119,37 @@ with gr.Blocks(theme=custom_theme) as demo:
110
 
111
  with gr.Column():
112
  with gr.Accordion("Click to View Source", open=False):
113
- translate_res_2 = gr.Textbox(
114
  label=f"Source Url"
115
  )
116
 
 
 
 
 
 
 
 
 
 
 
117
  with gr.Row():
118
  with gr.Column():
119
  query_match_out_3 = gr.Textbox(label=f"Search Result 3")
120
 
121
  with gr.Column():
122
  with gr.Accordion("Click to View Source", open=False):
123
- translate_res_3 = gr.Textbox(
124
  label=f"Source Url"
125
  )
 
 
 
 
 
 
 
 
126
 
127
  with gr.TabItem("Q&A"):
128
  gr.HTML(
@@ -279,12 +306,41 @@ with gr.Blocks(theme=custom_theme) as demo:
279
  # clear the chatbot Q&A history when this button is clicked by the user
280
  clear.click(lambda: None, None, chatbot, queue=False)
281
 
 
 
 
 
 
 
 
 
282
  # run search if user submits query
283
  user_query.submit(
284
  cross_lingual_document_search,
285
  [user_query, num_search_results, lang_choices, text_match],
286
  [query_match_out_1, query_match_out_2, query_match_out_3, \
287
- translate_res_1,translate_res_2,translate_res_3],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  queue=False,
289
  )
290
 
 
8
  load_history,
9
  load_science,
10
  )
11
+ from src.wiki_search import cross_lingual_document_search, translate_text
12
  from src.theme import CustomTheme
13
 
14
+
15
  max_search_results = 3
16
 
17
 
 
28
  history[-1][1] = bot_message
29
  return history
30
 
31
+ # def translate_text(doc):
32
+ # translator = EasyGoogleTranslate()
33
 
34
+ # doc = " ".join(doc.split()[:4800])
35
+ # result = translator.translate(doc, target_language='en')
36
+ # return result
37
+
38
 
39
  custom_theme = CustomTheme()
40
 
 
77
  with gr.Column():
78
  user_query = gr.Text(
79
  label="Enter query here",
80
+ placeholder="Search through study materials (e.g The Nigerian Civil War)",
81
  )
82
 
83
  num_search_results = gr.Slider(
 
99
 
100
  with gr.Column():
101
  with gr.Accordion("Click to View Source", open=False):
102
+
103
+ source_res_1 = gr.Textbox(
104
+ label=f"Source Url",
 
 
105
 
106
+ )
107
+ translate_btn_1 = gr.Button(
108
+ label="Translate Text",
109
+ value="Translate Text",
110
+ variant="primary",
111
+ )
112
  translate_res_1 = gr.Textbox(
113
+ label=f"Translation in English",
114
  )
115
 
116
  with gr.Row():
 
119
 
120
  with gr.Column():
121
  with gr.Accordion("Click to View Source", open=False):
122
+ source_res_2 = gr.Textbox(
123
  label=f"Source Url"
124
  )
125
 
126
+ translate_btn_2 = gr.Button(
127
+ label="Translate Text",
128
+ value="Translate Text",
129
+ variant="primary",
130
+ )
131
+ translate_res_2 = gr.Textbox(
132
+ label=f"Translation in English",
133
+
134
+ )
135
+
136
  with gr.Row():
137
  with gr.Column():
138
  query_match_out_3 = gr.Textbox(label=f"Search Result 3")
139
 
140
  with gr.Column():
141
  with gr.Accordion("Click to View Source", open=False):
142
+ source_res_3 = gr.Textbox(
143
  label=f"Source Url"
144
  )
145
+ translate_btn_3 = gr.Button(
146
+ label="Translate Text",
147
+ value="Translate Text",
148
+ variant="primary",
149
+ )
150
+ translate_res_3= gr.Textbox(
151
+ label=f"Translation in English",
152
+ )
153
 
154
  with gr.TabItem("Q&A"):
155
  gr.HTML(
 
306
  # clear the chatbot Q&A history when this button is clicked by the user
307
  clear.click(lambda: None, None, chatbot, queue=False)
308
 
309
+ # # run search as user is typing the query
310
+ # user_query.change(
311
+ # cross_lingual_document_search,
312
+ # [user_query, num_search_results, lang_choices, text_match],
313
+ # [query_match_out_1, query_match_out_2, query_match_out_3],
314
+ # queue=False,
315
+ # )
316
+
317
  # run search if user submits query
318
  user_query.submit(
319
  cross_lingual_document_search,
320
  [user_query, num_search_results, lang_choices, text_match],
321
  [query_match_out_1, query_match_out_2, query_match_out_3, \
322
+ source_res_1,source_res_2,source_res_3],
323
+ queue=False,
324
+ )
325
+
326
+
327
+ # translate results corresponding to 1st search result obtained if user clicks 'Translate'
328
+ translate_btn_1.click(
329
+ translate_text,
330
+ [query_match_out_1],
331
+ [translate_res_1],
332
+ queue=False,
333
+ )
334
+ translate_btn_2.click(
335
+ translate_text,
336
+ [query_match_out_2],
337
+ [translate_res_2],
338
+ queue=False,
339
+ )
340
+ translate_btn_3.click(
341
+ translate_text,
342
+ [query_match_out_3],
343
+ [translate_res_3],
344
  queue=False,
345
  )
346
 
requirements.txt CHANGED
@@ -3,3 +3,4 @@ gradio
3
  langchain
4
  black
5
  pinecone-client[grpc]
 
 
3
  langchain
4
  black
5
  pinecone-client[grpc]
6
+ easygoogletranslate
src/__pycache__/wiki_search.cpython-311.pyc CHANGED
Binary files a/src/__pycache__/wiki_search.cpython-311.pyc and b/src/__pycache__/wiki_search.cpython-311.pyc differ
 
src/wiki_search.py CHANGED
@@ -2,6 +2,8 @@ import os
2
  import cohere
3
  from typing import List
4
  import pinecone
 
 
5
 
6
  # load environment variables
7
  PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
@@ -15,6 +17,8 @@ COLLECTION = "wiki-embed"
15
  # create qdrant and cohere client
16
  cohere_client = cohere.Client(COHERE_API_KEY)
17
 
 
 
18
  def init_pinecone():
19
  pinecone.init(api_key= PINECONE_API_KEY,
20
  environment=PINECONE_ENV)
@@ -110,8 +114,10 @@ def document_source(
110
  return results
111
 
112
 
113
- def translate_search_result():
114
- pass
 
 
115
 
116
  if __name__ == "__main__":
117
  # query_embedding, user_query = embed_user_query("Who is the president of Nigeria")
 
2
  import cohere
3
  from typing import List
4
  import pinecone
5
+ from easygoogletranslate import EasyGoogleTranslate
6
+
7
 
8
  # load environment variables
9
  PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
 
17
  # create qdrant and cohere client
18
  cohere_client = cohere.Client(COHERE_API_KEY)
19
 
20
+ translator = EasyGoogleTranslate()
21
+
22
  def init_pinecone():
23
  pinecone.init(api_key= PINECONE_API_KEY,
24
  environment=PINECONE_ENV)
 
114
  return results
115
 
116
 
117
+ def translate_text(doc):
118
+ doc = " ".join(doc.split()[:4800])
119
+ result = translator.translate(doc, target_language='en')
120
+ return result
121
 
122
  if __name__ == "__main__":
123
  # query_embedding, user_query = embed_user_query("Who is the president of Nigeria")