Amandeep01 commited on
Commit
6aae403
·
verified ·
1 Parent(s): d2870dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -26
app.py CHANGED
@@ -1,36 +1,59 @@
1
  import gradio as gr
2
- import pandas as pd
 
 
3
  import numpy as np
4
- import pickle
5
 
6
- def predict_diabetes(pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, diabetes_pedigree_function, age):
7
- # Load the model
8
- with open("model.pkl", "rb") as f:
9
- model = pickle.load(f)
10
 
11
- input_data = np.array([[pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, diabetes_pedigree_function, age]])
12
- prediction = model.predict(input_data)
13
- result = "Diabetic" if prediction[0] == 1 else "Not Diabetic"
14
- return result
15
 
16
- # Gradio Interface
17
- description = "Diabetes Prediction Model - Enter your values below"
18
 
19
- demo = gr.Interface(
20
- fn=predict_diabetes,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  inputs=[
22
- gr.Number(label="Pregnancies"),
23
- gr.Number(label="Glucose"),
24
- gr.Number(label="Blood Pressure"),
25
- gr.Number(label="Skin Thickness"),
26
- gr.Number(label="Insulin"),
27
- gr.Number(label="BMI"),
28
- gr.Number(label="Diabetes Pedigree Function"),
29
- gr.Number(label="Age")
30
  ],
31
- outputs=gr.Text(label="Prediction"),
32
- title="Diabetes Prediction",
33
- description=description
34
  )
35
 
36
- demo.launch()
 
 
1
  import gradio as gr
2
+ import easyocr
3
+ from deep_translator import GoogleTranslator
4
+ from PIL import Image, ImageDraw, ImageFont
5
  import numpy as np
 
6
 
7
+ # Initialize reader with allowed lang_list only
8
+ reader = easyocr.Reader(['en', 'hi', 'mr', 'ne'])
 
 
9
 
10
+ # Font path works on HuggingFace
11
+ FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
 
 
12
 
13
+ def process_image(image, target_lang):
14
+ img_array = np.array(image)
15
 
16
+ # Run OCR safely
17
+ results = reader.readtext(img_array)
18
+
19
+ if not results:
20
+ return image, "No text detected"
21
+
22
+ final_translations = []
23
+ img_pil = image.convert("RGB")
24
+ draw = ImageDraw.Draw(img_pil)
25
+ font = ImageFont.truetype(FONT_PATH, size=20)
26
+
27
+ for (bbox, text, prob) in results:
28
+ if text.strip():
29
+ try:
30
+ translated = GoogleTranslator(source='auto', target=target_lang).translate(text)
31
+ except:
32
+ translated = "[Translation Error]"
33
+
34
+ final_translations.append(translated)
35
+
36
+ # Draw translation at top-left of bounding box
37
+ top_left = bbox[0]
38
+ draw.text((top_left[0], top_left[1] - 20), translated, fill=(255, 0, 0), font=font)
39
+
40
+ full_text = "\n".join(final_translations)
41
+ return img_pil, full_text
42
+
43
+ # Gradio UI
44
+ iface = gr.Interface(
45
+ fn=process_image,
46
  inputs=[
47
+ gr.Image(type="pil", label="Upload Image"),
48
+ gr.Textbox(label="Translate To (hi / en / mr / ne)")
49
+ ],
50
+ outputs=[
51
+ gr.Image(type="pil", label="Image with Translated Overlay"),
52
+ gr.Textbox(label="Translated Text Output")
 
 
53
  ],
54
+ title="Image Translator with Overlay",
55
+ description="Extract & translate text from an image (OCR + translation overlay)"
 
56
  )
57
 
58
+ if __name__ == "__main__":
59
+ iface.launch()