Spaces:
Runtime error
Runtime error
tonic
commited on
Commit
•
0878198
1
Parent(s):
9ffc199
adding application logic
Browse files- api_keys.env +2 -0
- app.py +179 -5
- requirements.txt +2 -0
api_keys.env
CHANGED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
COHERE_API_KEY=yhA2Q
|
2 |
+
SEAMLESSM4T=https://facebook-seamless-m4t-v2-large.hf.space/--replicas/nq5nn/
|
app.py
CHANGED
@@ -5,10 +5,184 @@ 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 |
|
9 |
-
|
10 |
-
|
11 |
-
det_processor, det_model = load_det_processor(), load_det_model()
|
12 |
-
rec_model, rec_processor = load_rec_model(), load_rec_processor()
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 gradio_client import Client
|
9 |
+
import requests
|
10 |
+
from io import BytesIO
|
11 |
+
import cohere
|
12 |
|
13 |
+
title = "# Welcome to AyaTonic"
|
14 |
+
description = "Learn a New Language With Aya"
|
|
|
|
|
15 |
|
16 |
+
# Load environment variables
|
17 |
+
load_dotenv()
|
18 |
+
COHERE_API_KEY = os.getenv('COHERE_API_KEY')
|
19 |
+
SEAMLESSM4T = os.getenv('SEAMLESSM4T')
|
20 |
+
|
21 |
+
co = cohere.Client(COHERE_API_KEY)
|
22 |
+
audio_client = Client(SEAMLESSM4T)
|
23 |
+
|
24 |
+
def process_audio_to_text(audio_path):
|
25 |
+
"""
|
26 |
+
Convert audio input to text using the Gradio client.
|
27 |
+
"""
|
28 |
+
result = audio_client.predict(
|
29 |
+
audio_path,
|
30 |
+
"English",
|
31 |
+
"English",
|
32 |
+
api_name="/s2tt"
|
33 |
+
)
|
34 |
+
return result['text'] # Adjust based on the actual response
|
35 |
+
|
36 |
+
def process_text_to_audio(text, target_language="English"):
|
37 |
+
"""
|
38 |
+
Convert text input to audio using the Gradio client.
|
39 |
+
"""
|
40 |
+
result = audio_client.predict(
|
41 |
+
text,
|
42 |
+
"English",
|
43 |
+
target_language,
|
44 |
+
api_name="/t2st"
|
45 |
+
)
|
46 |
+
return result['audio'] # Adjust based on the actual response
|
47 |
+
|
48 |
+
class OCRProcessor:
|
49 |
+
def __init__(self, langs=["en"]):
|
50 |
+
self.langs = langs
|
51 |
+
self.det_processor, self.det_model = load_det_processor(), load_det_model()
|
52 |
+
self.rec_model, self.rec_processor = load_rec_model(), load_rec_processor()
|
53 |
+
|
54 |
+
def process_image(self, image):
|
55 |
+
"""
|
56 |
+
Process a PIL image and return the OCR text.
|
57 |
+
"""
|
58 |
+
predictions = run_ocr([image], self.langs, self.det_model, self.det_processor, self.rec_model, self.rec_processor)
|
59 |
+
return predictions[0] # Assuming the first item in predictions contains the desired text
|
60 |
+
|
61 |
+
def process_pdf(self, pdf_path):
|
62 |
+
"""
|
63 |
+
Process a PDF file and return the OCR text.
|
64 |
+
"""
|
65 |
+
predictions = run_ocr([pdf_path], self.langs, self.det_model, self.det_processor, self.rec_model, self.rec_processor)
|
66 |
+
return predictions[0] # Assuming the first item in predictions contains the desired text
|
67 |
+
|
68 |
+
def process_input(image=None, file=None, audio=None, text=""):
|
69 |
+
final_text = text
|
70 |
+
if image is not None:
|
71 |
+
final_text += "\n" + ocr_processor.process_image(image)
|
72 |
+
if file is not None:
|
73 |
+
if file.name.lower().endswith(('.png', '.jpg', '.jpeg')):
|
74 |
+
pil_image = Image.open(file)
|
75 |
+
final_text += "\n" + ocr_processor.process_image(pil_image)
|
76 |
+
elif file.name.lower().endswith('.pdf'):
|
77 |
+
final_text += "\n" + ocr_processor.process_pdf(file.name)
|
78 |
+
else:
|
79 |
+
final_text += "\nUnsupported file type."
|
80 |
+
if audio is not None:
|
81 |
+
audio_text = process_audio_to_text(audio)
|
82 |
+
final_text += "\n" + audio_text
|
83 |
+
|
84 |
+
response = co.generate(
|
85 |
+
model='c4ai-aya',
|
86 |
+
prompt=final_text,
|
87 |
+
max_tokens=1024,
|
88 |
+
temperature=0.5
|
89 |
+
)
|
90 |
+
generated_text = response.generations[0].text
|
91 |
+
|
92 |
+
# Process generated text with command-nightly model
|
93 |
+
response = co.generate(
|
94 |
+
model='command-nightly',
|
95 |
+
prompt=generated_text,
|
96 |
+
max_tokens=1024,
|
97 |
+
temperature=0.5
|
98 |
+
)
|
99 |
+
processed_text = response.generations[0].text
|
100 |
+
|
101 |
+
audio_output = process_text_to_audio(processed_text)
|
102 |
+
|
103 |
+
return processed_text, audio_output
|
104 |
+
|
105 |
+
# Define Gradio interface
|
106 |
+
iface = gr.Interface(
|
107 |
+
fn=process_input,
|
108 |
+
inputs=[
|
109 |
+
gr.inputs.Image(type="pil", label="Camera Input"),
|
110 |
+
gr.inputs.File(label="File Upload"),
|
111 |
+
gr.inputs.Audio(source="microphone", type="filepath", label="Mic Input"),
|
112 |
+
gr.inputs.Textbox(lines=2, label="Text Input")
|
113 |
+
],
|
114 |
+
outputs=[
|
115 |
+
RichTextbox(label="Processed Text"),
|
116 |
+
gr.outputs.Audio(label="Audio Output")
|
117 |
+
],
|
118 |
+
title=title,
|
119 |
+
description=description
|
120 |
+
)
|
121 |
+
|
122 |
+
if __name__ == "__main__":
|
123 |
+
iface.launch()
|
124 |
+
|
125 |
+
|
126 |
+
# co = cohere.Client('yhA228YGeZSl1ctten8LQxw2dky2nngHetXFjV2Q') # This is your trial API key
|
127 |
+
# response = co.generate(
|
128 |
+
# model='c4ai-aya',
|
129 |
+
# prompt='एक यांत्रिक घड़ी दिन के समय को प्रदान करने के लिए एक गैर-इलेक्ट्रॉनिक तंत्र का उपयोग करती है। एक मुख्य स्प्रिंग का उपयोग यांत्रिक तंत्र को ऊर्जा संग्रहीत करने के लिए किया जाता है। एक यांत्रिक घड़ी में द��ंतों का एक कुंडल होता है जो धीरे-धीरे मुख्य स्प्रिंग से संचालित होता है। दांतों के कुंडल को एक यांत्रिक तंत्र में स्थानांतरित करने के लिए पहियों की एक श्रृंखला का उपयोग किया जाता है जो हाथों को घड़ी के चेहरे पर दाईं ओर ले जाता है। घड़ी के तंत्र को स्थिर करने और यह सुनिश्चित करने के लिए कि हाथ सही दिशा में घूमते हैं, एक कंपन का उपयोग किया जाता है।\n\nProduce a complete blog post in FRENCH based on the above : ',
|
130 |
+
# max_tokens=3674,
|
131 |
+
# temperature=0.9,
|
132 |
+
# k=0,
|
133 |
+
# stop_sequences=[],
|
134 |
+
# return_likelihoods='NONE')
|
135 |
+
# print('Prediction: {}'.format(response.generations[0].text))
|
136 |
+
|
137 |
+
# client = Client("https://facebook-seamless-m4t-v2-large.hf.space/--replicas/nq5nn/")
|
138 |
+
# result = client.predict(
|
139 |
+
# https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav, # filepath in 'Input speech' Audio component
|
140 |
+
# 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
|
141 |
+
# 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
|
142 |
+
# api_name="/s2st"
|
143 |
+
# )
|
144 |
+
# print(result)
|
145 |
+
|
146 |
+
# co = cohere.Client('yhA228YGeZSl1ctten8LQxw2dky2nngHetXFjV2Q')
|
147 |
+
# response = co.generate(
|
148 |
+
# model='command-nightly',
|
149 |
+
# 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:',
|
150 |
+
# max_tokens=7294,
|
151 |
+
# temperature=0.6,
|
152 |
+
# k=0,
|
153 |
+
# stop_sequences=[],
|
154 |
+
# return_likelihoods='NONE')
|
155 |
+
# print('Prediction: {}'.format(response.generations[0].text))
|
156 |
+
# example = RichTextbox().example_inputs()
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
iface = gr.Interface(
|
161 |
+
fn=process_input,
|
162 |
+
inputs=[
|
163 |
+
gr.Image(type="pil", label="Camera Input"),
|
164 |
+
gr.File(label="File Upload"),
|
165 |
+
gr.Audio(source="microphone", type="filepath", label="Mic Input"),
|
166 |
+
gr.Textbox(lines=2, label="Text Input")
|
167 |
+
],
|
168 |
+
outputs=[
|
169 |
+
gr.RichTextbox(label="Processed Text"),
|
170 |
+
gr.Audio(label="Audio Output")
|
171 |
+
],
|
172 |
+
title="OCR and Speech Processing App",
|
173 |
+
description="This app processes images, PDFs, and audio inputs to generate text and audio outputs."
|
174 |
+
)
|
175 |
+
|
176 |
+
if __name__ == "__main__":
|
177 |
+
iface.launch()
|
178 |
+
|
179 |
+
demo = gr.Interface(
|
180 |
+
lambda x:x,
|
181 |
+
RichTextbox(), # interactive version of your component
|
182 |
+
RichTextbox(), # static version of your component
|
183 |
+
examples=[[example]], # uncomment this line to view the "example version" of your component
|
184 |
+
)
|
185 |
+
|
186 |
+
|
187 |
+
if __name__ == "__main__":
|
188 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
gradio
|
2 |
gradio_rich_textbox
|
|
|
|
|
3 |
surya-ocr
|
4 |
pillow
|
|
|
1 |
gradio
|
2 |
gradio_rich_textbox
|
3 |
+
gradio-client
|
4 |
+
cohere
|
5 |
surya-ocr
|
6 |
pillow
|