BoojithDharshan commited on
Commit
edf3f45
·
verified ·
1 Parent(s): 2a757ba

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
+ import torch
4
+ from PIL import Image
5
+ from deep_translator import GoogleTranslator
6
+ from gradio.themes import Base
7
+
8
+ # Load BLIP model and processor
9
+ caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
10
+ caption_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
11
+
12
+ # Translator
13
+ translator = GoogleTranslator(source='en', target='hi')
14
+
15
+ # Device setup
16
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
+ caption_model.to(device)
18
+
19
+ def generate_caption(image):
20
+ try:
21
+ # Preprocess the image
22
+ inputs = caption_processor(images=image, return_tensors="pt")
23
+ pixel_values = inputs.pixel_values.to(device)
24
+
25
+ # Generate caption
26
+ output_ids = caption_model.generate(
27
+ pixel_values,
28
+ max_length=30,
29
+ num_beams=10,
30
+ num_beam_groups=2,
31
+ diversity_penalty=0.5,
32
+ repetition_penalty=2.0,
33
+ temperature=0.6,
34
+ top_k=50,
35
+ top_p=0.95,
36
+ no_repeat_ngram_size=3
37
+ )
38
+
39
+ # Decode English caption
40
+ english_caption = caption_processor.decode(output_ids[0], skip_special_tokens=True)
41
+
42
+ # Translate to Hindi
43
+ hindi_caption = translator.translate(english_caption)
44
+
45
+ return english_caption, hindi_caption
46
+
47
+ except Exception as e:
48
+ return f"Error: {str(e)}", f"Error: {str(e)}"
49
+
50
+ # Custom theme with a blue and white color scheme
51
+ custom_theme = gr.themes.Default(
52
+ primary_hue="blue", # Main color (buttons, highlights)
53
+ secondary_hue="gray", # Secondary elements
54
+ neutral_hue="slate", # Backgrounds, borders
55
+ text_size="lg", # Larger text for readability
56
+ radius_size="md", # Rounded corners
57
+ font=[gr.themes.GoogleFont("Roboto"), "sans-serif"] # Modern font
58
+ )
59
+
60
+ # Gradio interface with improved visuals
61
+ interface = gr.Interface(
62
+ fn=generate_caption,
63
+ inputs=gr.Image(type="pil", label="Upload an Image"),
64
+ outputs=[
65
+ gr.Textbox(label="English Caption", lines=2, placeholder="English caption will appear here..."),
66
+ gr.Textbox(label="Hindi Caption", lines=2, placeholder="हिंदी कैप्शन यहाँ दिखाई देगा...")
67
+ ],
68
+ title="Image Caption Generator (English & Hindi)",
69
+ description="Upload an image to generate captions in English and Hindi with a sleek, modern interface.",
70
+ theme=custom_theme,
71
+ css="""
72
+ .gradio-container { max-width: 800px; margin: auto; }
73
+ h1 { text-align: center; color: #1E40AF; }
74
+ .label { font-weight: bold; }
75
+ input, output { border-radius: 8px; }
76
+ """
77
+ )
78
+
79
+ interface.launch()