Amandeep01 commited on
Commit
d9c66cc
·
verified ·
1 Parent(s): bccdc8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -45,6 +45,10 @@ class HindiSignboardTranslator:
45
  str: Translated English text
46
  """
47
  try:
 
 
 
 
48
  # Tokenize and translate
49
  inputs = self.tokenizer(hindi_text, return_tensors="pt", padding=True)
50
  outputs = self.model.generate(**inputs)
@@ -52,7 +56,7 @@ class HindiSignboardTranslator:
52
  return english_text
53
  except Exception as e:
54
  print(f"Translation Error: {e}")
55
- return None
56
 
57
  def translate_signboard(self, image):
58
  """
@@ -62,6 +66,15 @@ class HindiSignboardTranslator:
62
  Returns:
63
  dict: Translation results
64
  """
 
 
 
 
 
 
 
 
 
65
  # Extract text via OCR
66
  hindi_text = self.extract_text(image)
67
 
@@ -79,7 +92,7 @@ class HindiSignboardTranslator:
79
  return {
80
  "status": "success",
81
  "original_text": hindi_text,
82
- "translated_text": english_text or "Translation failed"
83
  }
84
 
85
  # Initialize the translator
@@ -106,10 +119,7 @@ iface = gr.Interface(
106
  ],
107
  title="Hindi Signboard Translator",
108
  description="Upload a Hindi signboard image to extract and translate its text.",
109
- examples=[
110
- ["example_signboard1.jpg"],
111
- ["example_signboard2.jpg"]
112
- ]
113
  )
114
 
115
  # Launch the app
 
45
  str: Translated English text
46
  """
47
  try:
48
+ # Handle empty or None input
49
+ if not hindi_text:
50
+ return "No text detected"
51
+
52
  # Tokenize and translate
53
  inputs = self.tokenizer(hindi_text, return_tensors="pt", padding=True)
54
  outputs = self.model.generate(**inputs)
 
56
  return english_text
57
  except Exception as e:
58
  print(f"Translation Error: {e}")
59
+ return "Translation failed"
60
 
61
  def translate_signboard(self, image):
62
  """
 
66
  Returns:
67
  dict: Translation results
68
  """
69
+ # Validate input
70
+ if image is None:
71
+ return {
72
+ "status": "error",
73
+ "message": "No image provided",
74
+ "original_text": "",
75
+ "translated_text": ""
76
+ }
77
+
78
  # Extract text via OCR
79
  hindi_text = self.extract_text(image)
80
 
 
92
  return {
93
  "status": "success",
94
  "original_text": hindi_text,
95
+ "translated_text": english_text
96
  }
97
 
98
  # Initialize the translator
 
119
  ],
120
  title="Hindi Signboard Translator",
121
  description="Upload a Hindi signboard image to extract and translate its text.",
122
+ # Removed example images
 
 
 
123
  )
124
 
125
  # Launch the app