Spaces:
Runtime error
Runtime error
tonic
commited on
Commit
•
43104b8
1
Parent(s):
4062737
language list and refactor interface + bala's bugfixes
Browse files- app.py +42 -94
- requirements.txt +2 -1
app.py
CHANGED
@@ -5,7 +5,7 @@ from surya.ocr import run_ocr
|
|
5 |
from surya.model.detection.segformer import load_model as load_det_model, load_processor as load_det_processor
|
6 |
from surya.model.recognition.model import load_model as load_rec_model
|
7 |
from surya.model.recognition.processor import load_processor as load_rec_processor
|
8 |
-
from lang_list import
|
9 |
from gradio_client import Client
|
10 |
from dotenv import load_dotenv
|
11 |
import requests
|
@@ -13,6 +13,7 @@ from io import BytesIO
|
|
13 |
import cohere
|
14 |
import os
|
15 |
import re
|
|
|
16 |
|
17 |
|
18 |
title = "# Welcome to AyaTonic"
|
@@ -21,6 +22,7 @@ description = "Learn a New Language With Aya"
|
|
21 |
load_dotenv()
|
22 |
COHERE_API_KEY = os.getenv('CO_API_KEY')
|
23 |
SEAMLESSM4T = os.getenv('SEAMLESSM4T')
|
|
|
24 |
|
25 |
inputlanguage = ""
|
26 |
producetext = "\n\nProduce a complete expositional blog post in {target_language} based on the above :"
|
@@ -68,11 +70,13 @@ class TaggedPhraseExtractor:
|
|
68 |
|
69 |
co = cohere.Client(COHERE_API_KEY)
|
70 |
audio_client = Client(SEAMLESSM4T)
|
|
|
71 |
|
72 |
def process_audio_to_text(audio_path, inputlanguage="English"):
|
73 |
"""
|
74 |
Convert audio input to text using the Gradio client.
|
75 |
"""
|
|
|
76 |
result = audio_client.predict(
|
77 |
audio_path,
|
78 |
inputlanguage,
|
@@ -80,19 +84,20 @@ def process_audio_to_text(audio_path, inputlanguage="English"):
|
|
80 |
api_name="/s2tt"
|
81 |
)
|
82 |
print("Audio Result: ", result)
|
83 |
-
return result[
|
84 |
|
85 |
-
def process_text_to_audio(text,
|
86 |
"""
|
87 |
Convert text input to audio using the Gradio client.
|
88 |
"""
|
|
|
89 |
result = audio_client.predict(
|
90 |
text,
|
91 |
-
|
92 |
-
|
93 |
api_name="/t2st"
|
94 |
)
|
95 |
-
return result[
|
96 |
|
97 |
class OCRProcessor:
|
98 |
def __init__(self, langs=["en"]):
|
@@ -114,7 +119,7 @@ class OCRProcessor:
|
|
114 |
predictions = run_ocr([pdf_path], [self.langs], self.det_model, self.det_processor, self.rec_model, self.rec_processor)
|
115 |
return predictions[0] # Assuming the first item in predictions contains the desired text
|
116 |
|
117 |
-
def process_input(image=None, file=None, audio=None, text=""):
|
118 |
ocr_processor = OCRProcessor()
|
119 |
final_text = text
|
120 |
if image is not None:
|
@@ -164,94 +169,37 @@ def process_input(image=None, file=None, audio=None, text=""):
|
|
164 |
)
|
165 |
processed_text = response.generations[0].text
|
166 |
|
167 |
-
audio_output = process_text_to_audio(processed_text)
|
168 |
|
169 |
return processed_text, audio_output
|
170 |
-
# Define Gradio interface
|
171 |
-
iface = gr.Interface(
|
172 |
-
fn=process_input,
|
173 |
-
inputs=[
|
174 |
-
gr.Image(type="pil", label="Camera Input"),
|
175 |
-
gr.File(label="File Upload"),
|
176 |
-
gr.Audio(sources="microphone", type="filepath", label="Mic Input"),
|
177 |
-
gr.Textbox(lines=2, label="Text Input"),
|
178 |
-
gr.Dropdown(choices=TEXT_SOURCE_LANGUAGE_NAMES, label="Input Language"),
|
179 |
-
gr.Dropdown(choices=TEXT_SOURCE_LANGUAGE_NAMES, label="Target Language")
|
180 |
-
],
|
181 |
-
outputs=[
|
182 |
-
RichTextbox(label="Processed Text"),
|
183 |
-
gr.Audio(label="Audio Output")
|
184 |
-
],
|
185 |
-
title=title,
|
186 |
-
description=description
|
187 |
-
)
|
188 |
-
|
189 |
-
if __name__ == "__main__":
|
190 |
-
iface.launch()
|
191 |
-
|
192 |
-
|
193 |
-
# co = cohere.Client('yhA228YGeZSl1ctten8LQxw2dky2nngHetXFjV2Q') # This is your trial API key
|
194 |
-
# response = co.generate(
|
195 |
-
# model='c4ai-aya',
|
196 |
-
# prompt='एक यांत्रिक घड़ी दिन के समय को प्रदान करने के लिए एक गैर-इलेक्ट्रॉनिक तंत्र का उपयोग करती है। एक मुख्य स्प्रिंग का उपयोग यांत्रिक तंत्र को ऊर्जा संग्रहीत करने के लिए किया जाता है। एक यांत्रिक घड़ी में दांतों का एक कुंडल होता है जो धीरे-धीरे मुख्य स्प्रिंग से संचालित होता है। दांतों के कुंडल को एक यांत्रिक तंत्र में स्थानांतरित करने के लिए पहियों की एक श्रृंखला का उपयोग किया जाता है जो हाथों को घड़ी के चेहरे पर दाईं ओर ले जाता है। घड़ी के तंत्र को स्थिर करने और यह सुनिश्चित करने के लिए कि हाथ सही दिशा में घूमते हैं, एक कंपन का उपयोग किया जाता है। ',
|
197 |
-
# max_tokens=3674,
|
198 |
-
# temperature=0.9,
|
199 |
-
# k=0,
|
200 |
-
# stop_sequences=[],
|
201 |
-
# return_likelihoods='NONE')
|
202 |
-
# print('Prediction: {}'.format(response.generations[0].text))
|
203 |
-
|
204 |
-
# client = Client("https://facebook-seamless-m4t-v2-large.hf.space/--replicas/nq5nn/")
|
205 |
-
# result = client.predict(
|
206 |
-
# https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav, # filepath in 'Input speech' Audio component
|
207 |
-
# Afrikaans, # Literal[Afrikaans, Amharic, Armenian, Assamese, Basque, Belarusian, Bengali, Bosnian, Bulgarian, Burmese, Cantonese, Catalan, Cebuano, Central Kurdish, Croatian, Czech, Danish, Dutch, Egyptian Arabic, English, Estonian, Finnish, French, Galician, Ganda, Georgian, German, Greek, Gujarati, Halh Mongolian, Hebrew, Hindi, Hungarian, Icelandic, Igbo, Indonesian, Irish, Italian, Japanese, Javanese, Kannada, Kazakh, Khmer, Korean, Kyrgyz, Lao, Lithuanian, Luo, Macedonian, Maithili, Malayalam, Maltese, Mandarin Chinese, Marathi, Meitei, Modern Standard Arabic, Moroccan Arabic, Nepali, North Azerbaijani, Northern Uzbek, Norwegian Bokmål, Norwegian Nynorsk, Nyanja, Odia, Polish, Portuguese, Punjabi, Romanian, Russian, Serbian, Shona, Sindhi, Slovak, Slovenian, Somali, Southern Pashto, Spanish, Standard Latvian, Standard Malay, Swahili, Swedish, Tagalog, Tajik, Tamil, Telugu, Thai, Turkish, Ukrainian, Urdu, Vietnamese, Welsh, West Central Oromo, Western Persian, Yoruba, Zulu] in 'Source language' Dropdown component
|
208 |
-
# Bengali, # Literal[Bengali, Catalan, Czech, Danish, Dutch, English, Estonian, Finnish, French, German, Hindi, Indonesian, Italian, Japanese, Korean, Maltese, Mandarin Chinese, Modern Standard Arabic, Northern Uzbek, Polish, Portuguese, Romanian, Russian, Slovak, Spanish, Swahili, Swedish, Tagalog, Telugu, Thai, Turkish, Ukrainian, Urdu, Vietnamese, Welsh, Western Persian] in 'Target language' Dropdown component
|
209 |
-
# api_name="/s2st"
|
210 |
-
# )
|
211 |
-
# print(result)
|
212 |
-
|
213 |
-
# co = cohere.Client('yhA228YGeZSl1ctten8LQxw2dky2nngHetXFjV2Q')
|
214 |
-
# response = co.generate(
|
215 |
-
# model='command-nightly',
|
216 |
-
# prompt='Les mécanismes de montres mécaniques\n\nLes mécanismes de montres mécaniques sont des mécanismes qui indiquent la journée, mais pas l\'électronique. Elles utilisent un ressort principal pour stocker l\'énergie nécessaire au fonctionnement des mécanismes. Un train d\'engrenages est utilisé pour transférer l\'énergie du ressort principal à un ensemble de roues qui font tourner les aiguilles dans le sens horaire sur le cadran de la montre.\n\nLes mécanismes de montres mécaniques sontdakshineswar omkarnathji, qui sont des lieux de culte qui sont construits dans le temple. Les engrenages sont des roues qui sont utilisées pour transférer l\'énergie du ressort principal à un ensemble de roues qui font tourner les aiguilles dans le sens horaire sur le cadran de la montre.\n\nLe ressort principal est un ressort qui est utilisé pour stocker l\'énergie nécessaire au fonctionnement des mécanismes de la montre. Le ressort principal est un ressort qui est utilisé pour stocker l\'énergie nécessaire au fonctionnement des mécanismes de la montre, et il est utilisé pour transférer l\'énergie aux engrenages, qui sont des roues qui sont utilisées pour faire tourner les aiguilles dans le sens horaire sur le cadran de la montre.\n\nLes engrenages sont des roues qui sont utilisées pour faire tourner les aiguilles dans le sens horaire sur le cadran de la montre, et elles sont utilisées pour transférer l\'énergie du ressort principal aux roues qui font tourner les aiguilles dans le sens horaire sur le cadran de la montre.\n\nLes mécanismes de montres mécaniques sont des mécanismes qui indiquent la journée, et elles sont utilisées pour transférer l\'énergie du ressort principal à un ensemble de roues qui font tourner les aiguilles dans le sens horaire sur le cadran de la montre.\n\nLes mécanismes de montres mécaniques sont des mécanismes qui indiquent la journée, et elles sont utilisées pour transférer l\'énergie du ressort principal à un ensemble de roues qui font tourner les aiguilles dans le sens horaire sur le cadran de la montre, et elles sont utilisées pour stabiliser le mécanisme de la montre, et pour s\'assurer que les aiguilles tournent dans le bon sens.\n\nthe above text is a learning aid. you must use rich text format to rewrite the above and add 1 . a red color tags for nouns 2. a blue color tag for verbs 3. a green color tag for adjectives and adverbs:',
|
217 |
-
# max_tokens=7294,
|
218 |
-
# temperature=0.6,
|
219 |
-
# k=0,
|
220 |
-
# stop_sequences=[],
|
221 |
-
# return_likelihoods='NONE')
|
222 |
-
# print('Prediction: {}'.format(response.generations[0].text))
|
223 |
-
# example = RichTextbox().example_inputs()
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
-
|
227 |
-
|
228 |
-
# fn=process_input,
|
229 |
-
# inputs=[
|
230 |
-
# gr.Image(type="pil", label="Camera Input"),
|
231 |
-
# gr.File(label="File Upload"),
|
232 |
-
# gr.Audio(sources="microphone", type="filepath", label="Mic Input"),
|
233 |
-
# gr.Textbox(lines=2, label="Text Input"),
|
234 |
-
# gr.Dropdown(choices=TEXT_SOURCE_LANGUAGE_NAMES, label="Input Language"),
|
235 |
-
# gr.Dropdown(choices=TEXT_SOURCE_LANGUAGE_NAMES, label="Target Language")
|
236 |
-
# ],
|
237 |
-
# outputs=[
|
238 |
-
# gr.RichTextbox(label="Processed Text"),
|
239 |
-
# gr.Audio(label="Audio Output")
|
240 |
-
# ],
|
241 |
-
# title="OCR and Speech Processing App",
|
242 |
-
# description="This app processes images, PDFs, and audio inputs to generate text and audio outputs."
|
243 |
-
# )
|
244 |
-
|
245 |
-
# if __name__ == "__main__":
|
246 |
-
# # iface.launch()
|
247 |
-
|
248 |
-
# demo = gr.Interface(
|
249 |
-
# lambda x:x,
|
250 |
-
# RichTextbox(), # interactive version of your component
|
251 |
-
# RichTextbox(), # static version of your component
|
252 |
-
# examples=[[example]], # uncomment this line to view the "example version" of your component
|
253 |
-
# )
|
254 |
-
|
255 |
-
|
256 |
-
# if __name__ == "__main__":
|
257 |
-
# demo.launch()
|
|
|
5 |
from surya.model.detection.segformer import load_model as load_det_model, load_processor as load_det_processor
|
6 |
from surya.model.recognition.model import load_model as load_rec_model
|
7 |
from surya.model.recognition.processor import load_processor as load_rec_processor
|
8 |
+
from lang_list import TEXT_SOURCE_LANGUAGE_NAMES
|
9 |
from gradio_client import Client
|
10 |
from dotenv import load_dotenv
|
11 |
import requests
|
|
|
13 |
import cohere
|
14 |
import os
|
15 |
import re
|
16 |
+
import pandas as pd
|
17 |
|
18 |
|
19 |
title = "# Welcome to AyaTonic"
|
|
|
22 |
load_dotenv()
|
23 |
COHERE_API_KEY = os.getenv('CO_API_KEY')
|
24 |
SEAMLESSM4T = os.getenv('SEAMLESSM4T')
|
25 |
+
df = pd.read_csv("lang_list.csv")
|
26 |
|
27 |
inputlanguage = ""
|
28 |
producetext = "\n\nProduce a complete expositional blog post in {target_language} based on the above :"
|
|
|
70 |
|
71 |
co = cohere.Client(COHERE_API_KEY)
|
72 |
audio_client = Client(SEAMLESSM4T)
|
73 |
+
# client = Client(SEAMLESSM4T)
|
74 |
|
75 |
def process_audio_to_text(audio_path, inputlanguage="English"):
|
76 |
"""
|
77 |
Convert audio input to text using the Gradio client.
|
78 |
"""
|
79 |
+
audio_client = Client(SEAMLESSM4T)
|
80 |
result = audio_client.predict(
|
81 |
audio_path,
|
82 |
inputlanguage,
|
|
|
84 |
api_name="/s2tt"
|
85 |
)
|
86 |
print("Audio Result: ", result)
|
87 |
+
return result[0]
|
88 |
|
89 |
+
def process_text_to_audio(text, translatefrom, translateto):
|
90 |
"""
|
91 |
Convert text input to audio using the Gradio client.
|
92 |
"""
|
93 |
+
audio_client = Client(SEAMLESSM4T)
|
94 |
result = audio_client.predict(
|
95 |
text,
|
96 |
+
translatefrom,
|
97 |
+
translateto,
|
98 |
api_name="/t2st"
|
99 |
)
|
100 |
+
return result[0]
|
101 |
|
102 |
class OCRProcessor:
|
103 |
def __init__(self, langs=["en"]):
|
|
|
119 |
predictions = run_ocr([pdf_path], [self.langs], self.det_model, self.det_processor, self.rec_model, self.rec_processor)
|
120 |
return predictions[0] # Assuming the first item in predictions contains the desired text
|
121 |
|
122 |
+
def process_input(image=None, file=None, audio=None, text="", translateto = "English", translatefrom = "English" ):
|
123 |
ocr_processor = OCRProcessor()
|
124 |
final_text = text
|
125 |
if image is not None:
|
|
|
169 |
)
|
170 |
processed_text = response.generations[0].text
|
171 |
|
172 |
+
audio_output = process_text_to_audio(processed_text, translateto, translateto)
|
173 |
|
174 |
return processed_text, audio_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
+
def main():
|
177 |
+
with gr.Blocks() as demo:
|
178 |
+
gr.Markdown(title)
|
179 |
+
gr.Markdown(description)
|
180 |
+
|
181 |
+
with gr.Row():
|
182 |
+
input_language = gr.Dropdown(choices=df["name"].to_list(), label="Your Native Language")
|
183 |
+
target_language = gr.Dropdown(choices=df["name"].to_list(), label="Language To Learn")
|
184 |
+
|
185 |
+
with gr.Accordion("Talk To 🌟AyaTonic"):
|
186 |
+
with gr.Tab("🤙🏻Audio & Text"):
|
187 |
+
audio_input = gr.Audio(sources="microphone", type="filepath", label="Mic Input")
|
188 |
+
text_input = gr.Textbox(lines=2, label="Text Input")
|
189 |
+
with gr.Tab("📸Image & File"):
|
190 |
+
image_input = gr.Image(type="pil", label="Camera Input")
|
191 |
+
file_input = gr.File(label="File Upload")
|
192 |
+
|
193 |
+
process_button = gr.Button("🌟AyaTonic")
|
194 |
+
|
195 |
+
processed_text_output = RichTextbox(label="Processed Text")
|
196 |
+
audio_output = gr.Audio(label="Audio Output")
|
197 |
+
|
198 |
+
process_button.click(
|
199 |
+
fn=process_input,
|
200 |
+
inputs=[image_input, file_input, audio_input, text_input, input_language, target_language],
|
201 |
+
outputs=[processed_text_output, audio_output]
|
202 |
+
)
|
203 |
|
204 |
+
if __name__ == "__main__":
|
205 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -6,4 +6,5 @@ surya-ocr
|
|
6 |
pillow
|
7 |
torchvision
|
8 |
torch
|
9 |
-
python-dotenv
|
|
|
|
6 |
pillow
|
7 |
torchvision
|
8 |
torch
|
9 |
+
python-dotenv
|
10 |
+
pandas
|