Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,17 +5,42 @@ from transformers import pipeline
|
|
| 5 |
# ✅ Load OCR Model (Text Extraction)
|
| 6 |
ocr_pipeline = pipeline("image-to-text", model="microsoft/trocr-large-handwritten")
|
| 7 |
|
| 8 |
-
# ✅ Load Translation Model
|
| 9 |
-
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
|
| 10 |
|
| 11 |
def process_image(image):
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# ✅ Gradio Interface
|
| 21 |
iface = gr.Interface(
|
|
|
|
| 5 |
# ✅ Load OCR Model (Text Extraction)
|
| 6 |
ocr_pipeline = pipeline("image-to-text", model="microsoft/trocr-large-handwritten")
|
| 7 |
|
| 8 |
+
# ✅ Load Translation Model (English to Hindi)
|
| 9 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
|
| 10 |
|
| 11 |
def process_image(image):
|
| 12 |
+
try:
|
| 13 |
+
# Extract text from image
|
| 14 |
+
ocr_result = ocr_pipeline(image)
|
| 15 |
|
| 16 |
+
# Debug: Check OCR output
|
| 17 |
+
print("OCR Raw Output:", ocr_result)
|
| 18 |
|
| 19 |
+
# Ensure the OCR output is valid
|
| 20 |
+
if not ocr_result or "generated_text" not in ocr_result[0]:
|
| 21 |
+
return "Error: Could not extract text. Try another image.", ""
|
| 22 |
+
|
| 23 |
+
extracted_text = ocr_result[0]['generated_text']
|
| 24 |
+
print("Extracted Text:", extracted_text) # Debugging Output
|
| 25 |
+
|
| 26 |
+
# Handle empty extracted text
|
| 27 |
+
if not extracted_text.strip():
|
| 28 |
+
return "Error: No text detected in image.", ""
|
| 29 |
+
|
| 30 |
+
# Translate the extracted text
|
| 31 |
+
translated_result = translator(extracted_text)
|
| 32 |
+
|
| 33 |
+
# Debug: Check Translation output
|
| 34 |
+
print("Translation Raw Output:", translated_result)
|
| 35 |
+
|
| 36 |
+
translated_text = translated_result[0]['translation_text']
|
| 37 |
+
print("Translated Text:", translated_text) # Debugging Output
|
| 38 |
+
|
| 39 |
+
return extracted_text, translated_text
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print("Error:", str(e)) # Print error in the console
|
| 43 |
+
return f"Error: {str(e)}", ""
|
| 44 |
|
| 45 |
# ✅ Gradio Interface
|
| 46 |
iface = gr.Interface(
|