Spaces:
Sleeping
Sleeping
vanamuthuv
commited on
Commit
•
16bd4d9
1
Parent(s):
8063f5b
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,88 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import cv2
|
4 |
-
import numpy as np
|
5 |
-
from easyocr import Reader
|
6 |
-
import google.generativeai as genai
|
7 |
-
|
8 |
-
# Replace with the name of your secret
|
9 |
-
GENERATIVE_AI_API_KEY = os.getenv("GEMINI_API")
|
10 |
-
|
11 |
-
genai.configure(api_key=GENERATIVE_AI_API_KEY)
|
12 |
-
|
13 |
-
def detect_largest_font(image):
|
14 |
-
# Validate loaded image
|
15 |
-
if image is None:
|
16 |
-
print(f"Error: Unable to load image from {image}")
|
17 |
-
return None, None
|
18 |
-
if not isinstance(image, np.ndarray):
|
19 |
-
print("Error: Loaded data is not a NumPy array.")
|
20 |
-
return None, None
|
21 |
-
|
22 |
-
# Convert image to RGB (OpenCV uses BGR by default)
|
23 |
-
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
24 |
-
|
25 |
-
# Initialize EasyOCR reader
|
26 |
-
reader = Reader(['en']) # Specify languages for OCR
|
27 |
-
|
28 |
-
# Perform OCR on the image
|
29 |
-
result = reader.readtext(rgb_image)
|
30 |
-
|
31 |
-
largest_font_text = ""
|
32 |
-
largest_font_size = 0
|
33 |
-
|
34 |
-
for detection in result:
|
35 |
-
# Extract bounding box and text from detection
|
36 |
-
bbox = detection[0]
|
37 |
-
text = detection[1].strip()
|
38 |
-
|
39 |
-
# Calculate bounding box dimensions
|
40 |
-
x_min, y_min = bbox[0]
|
41 |
-
x_max, y_max = bbox[2]
|
42 |
-
w = x_max - x_min
|
43 |
-
h = y_max - y_min
|
44 |
-
|
45 |
-
# Calculate font size (approximation based on bounding box size)
|
46 |
-
font_size = min(w, h)
|
47 |
-
|
48 |
-
# Update largest font text if found a larger font size
|
49 |
-
if font_size > largest_font_size:
|
50 |
-
largest_font_size = font_size
|
51 |
-
largest_font_text = text
|
52 |
-
|
53 |
-
# Generate movie review using GenerativeAI
|
54 |
-
model = genai.GenerativeModel('gemini-1.0-pro-latest')
|
55 |
-
response = model.generate_content(f"In What Year the movie {largest_font_text} is released and how are the cast and what the movie is about?")
|
56 |
-
|
57 |
-
if getattr(response, '_done', False) and getattr(response, '_result', None): # Use getattr for flexibility
|
58 |
-
movie_review = response._result.candidates[0].content.parts[0].text
|
59 |
-
return largest_font_text, movie_review
|
60 |
-
else:
|
61 |
-
print("Error: Failed to generate movie review.")
|
62 |
-
return largest_font_text, None
|
63 |
-
|
64 |
-
# Gradio interface definition
|
65 |
-
interface = gr.Interface(
|
66 |
-
fn=detect_largest_font,
|
67 |
-
inputs=[
|
68 |
-
gr.Image(label="Upload Movie Poster"),
|
69 |
-
],
|
70 |
-
outputs=[ gr.Text(elem_id="Movie Title"), gr.Text(elem_id="Movie Review")],
|
71 |
-
title="Movie Title and Review from Poster",
|
72 |
-
description="Upload a movie poster and get the title in largest font and a review generated by AI.",
|
73 |
-
)
|
74 |
-
|
75 |
-
# Add CSS styling (optional)
|
76 |
-
interface.css = """
|
77 |
-
.gr-output-container {
|
78 |
-
display: flex;
|
79 |
-
flex-direction: column;
|
80 |
-
gap: 10px; /* Adjust spacing between outputs */
|
81 |
-
}
|
82 |
-
|
83 |
-
#extracted_title, #movie_review {
|
84 |
-
font-weight: bold;
|
85 |
-
}
|
86 |
-
"""
|
87 |
-
|
88 |
interface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from easyocr import Reader
|
6 |
+
import google.generativeai as genai
|
7 |
+
|
8 |
+
# Replace with the name of your secret
|
9 |
+
GENERATIVE_AI_API_KEY = os.getenv("GEMINI_API")
|
10 |
+
|
11 |
+
genai.configure(api_key=GENERATIVE_AI_API_KEY)
|
12 |
+
|
13 |
+
def detect_largest_font(image):
|
14 |
+
# Validate loaded image
|
15 |
+
if image is None:
|
16 |
+
print(f"Error: Unable to load image from {image}")
|
17 |
+
return None, None
|
18 |
+
if not isinstance(image, np.ndarray):
|
19 |
+
print("Error: Loaded data is not a NumPy array.")
|
20 |
+
return None, None
|
21 |
+
|
22 |
+
# Convert image to RGB (OpenCV uses BGR by default)
|
23 |
+
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
24 |
+
|
25 |
+
# Initialize EasyOCR reader
|
26 |
+
reader = Reader(['en']) # Specify languages for OCR
|
27 |
+
|
28 |
+
# Perform OCR on the image
|
29 |
+
result = reader.readtext(rgb_image)
|
30 |
+
|
31 |
+
largest_font_text = ""
|
32 |
+
largest_font_size = 0
|
33 |
+
|
34 |
+
for detection in result:
|
35 |
+
# Extract bounding box and text from detection
|
36 |
+
bbox = detection[0]
|
37 |
+
text = detection[1].strip()
|
38 |
+
|
39 |
+
# Calculate bounding box dimensions
|
40 |
+
x_min, y_min = bbox[0]
|
41 |
+
x_max, y_max = bbox[2]
|
42 |
+
w = x_max - x_min
|
43 |
+
h = y_max - y_min
|
44 |
+
|
45 |
+
# Calculate font size (approximation based on bounding box size)
|
46 |
+
font_size = min(w, h)
|
47 |
+
|
48 |
+
# Update largest font text if found a larger font size
|
49 |
+
if font_size > largest_font_size:
|
50 |
+
largest_font_size = font_size
|
51 |
+
largest_font_text = text
|
52 |
+
|
53 |
+
# Generate movie review using GenerativeAI
|
54 |
+
model = genai.GenerativeModel('gemini-1.0-pro-latest')
|
55 |
+
response = model.generate_content(f"In What Year the movie {largest_font_text} is released and how are the cast and what the movie is about?")
|
56 |
+
|
57 |
+
if getattr(response, '_done', False) and getattr(response, '_result', None): # Use getattr for flexibility
|
58 |
+
movie_review = response._result.candidates[0].content.parts[0].text
|
59 |
+
return largest_font_text, movie_review
|
60 |
+
else:
|
61 |
+
print("Error: Failed to generate movie review.")
|
62 |
+
return largest_font_text, None
|
63 |
+
|
64 |
+
# Gradio interface definition
|
65 |
+
interface = gr.Interface(
|
66 |
+
fn=detect_largest_font,
|
67 |
+
inputs=[
|
68 |
+
gr.Image(label="Upload Movie Poster"),
|
69 |
+
],
|
70 |
+
outputs=[ gr.Text(elem_id="Movie Title"), gr.Text(elem_id="Movie Review")],
|
71 |
+
title="Movie Title and Review from Poster",
|
72 |
+
description="Upload a movie poster and get the title in largest font and a review generated by AI.",
|
73 |
+
)
|
74 |
+
|
75 |
+
# Add CSS styling (optional)
|
76 |
+
interface.css = """
|
77 |
+
.gr-output-container {
|
78 |
+
display: flex;
|
79 |
+
flex-direction: column;
|
80 |
+
gap: 10px; /* Adjust spacing between outputs */
|
81 |
+
}
|
82 |
+
|
83 |
+
#extracted_title, #movie_review {
|
84 |
+
font-weight: bold;
|
85 |
+
}
|
86 |
+
"""
|
87 |
+
|
88 |
interface.launch()
|