abiabidali commited on
Commit
d327984
·
verified ·
1 Parent(s): aba7d30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -23
app.py CHANGED
@@ -1,32 +1,104 @@
 
 
1
  import pandas as pd
2
- from transformers import pipeline
 
 
 
 
3
 
4
- # Function to enhance title using Hugging Face model
5
- def generate_title_with_huggingface(description):
6
- result = generator(description, max_length=100, num_return_sequences=1)
7
- return result[0]['generated_text']
8
 
9
- # Load the CSV file directly (replace 'input.csv' with your actual file path)
10
- file_name = 'input.csv' # Update this with the correct path to your CSV file
11
- df = pd.read_csv(file_name)
12
 
13
- # Initialize the text generation model (e.g., GPT-2)
14
- generator = pipeline('text-generation', model='gpt-2')
15
 
16
- # Apply the function to generate SEO-friendly titles
17
- df['Title'] = df['Description'].apply(generate_title_with_huggingface)
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Function to generate keywords (basic example)
20
- def generate_keywords(description):
21
- words = set(description.replace(',', '').replace('.', '').split())
22
- return ",".join(list(words)[:50])
23
 
24
- # Generate basic keywords from the description (you can enhance this further)
25
- df['Keywords'] = df['Description'].apply(generate_keywords)
26
 
27
- # Save the SEO-optimized DataFrame to a new CSV file
28
- seo_output_file_path = 'seo_huggingface_filename_title_keywords.csv'
29
- df[['Filename', 'Title', 'Keywords']].to_csv(seo_output_file_path, index=False)
 
 
 
 
30
 
31
- # The output file will be saved in the current working directory
32
- print(f"File saved as {seo_output_file_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BlipProcessor, BlipForConditionalGeneration
2
+ from PIL import Image
3
  import pandas as pd
4
+ import numpy as np
5
+ import gradio as gr
6
+ import tempfile
7
+ import os
8
+ import csv
9
 
10
+ # Initialize the processor and model
11
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
12
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
 
13
 
14
+ def generate_image_caption(image):
15
+ """
16
+ Generate a caption for the given image.
17
 
18
+ Args:
19
+ image (PIL.Image): The image object.
20
 
21
+ Returns:
22
+ str: The generated caption.
23
+ """
24
+ # Convert image to RGB format
25
+ image = image.convert("RGB")
26
+
27
+ # Preprocess the image and generate a caption
28
+ inputs = processor(images=image, return_tensors="pt")
29
+ outputs = model.generate(**inputs)
30
+ caption = processor.decode(outputs[0], skip_special_tokens=True)
31
+
32
+ return caption
33
 
34
+ def generate_keywords(caption):
35
+ """
36
+ Generate a list of keywords from the caption.
 
37
 
38
+ Args:
39
+ caption (str): The image caption.
40
 
41
+ Returns:
42
+ list: A list of single-word keywords.
43
+ """
44
+ # Example simple keyword extraction (use a more sophisticated method if needed)
45
+ words = caption.split()
46
+ keywords = list(set(words))[:50] # Take unique words and limit to 50
47
+ return keywords
48
 
49
+ def process_images(image_files):
50
+ """
51
+ Process uploaded images to generate metadata and create a CSV file.
52
+
53
+ Args:
54
+ image_files (list of file-like objects): List of uploaded image files.
55
+
56
+ Returns:
57
+ tuple: A list of PIL images, path to the CSV file.
58
+ """
59
+ metadata = []
60
+ temp_dir = tempfile.mkdtemp()
61
+
62
+ for image_file in image_files:
63
+ filename = os.path.basename(image_file.name)
64
+ image = Image.open(image_file)
65
+ caption = generate_image_caption(image)
66
+ if caption:
67
+ keywords = generate_keywords(caption)
68
+ # Ensure the title is within the 70 to 100 characters range
69
+ title = caption if 70 <= len(caption) <= 100 else caption[:100]
70
+ metadata.append({
71
+ 'filename': filename,
72
+ 'title': title,
73
+ 'keywords': keywords
74
+ })
75
+
76
+ # Create CSV file
77
+ csv_file_path = os.path.join(temp_dir, 'images_metadata.csv')
78
+ with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file:
79
+ writer = csv.writer(file)
80
+ writer.writerow(['Filename', 'Title', 'Keywords'])
81
+ for data in metadata:
82
+ filename = data['filename']
83
+ title = data['title']
84
+ keywords = ','.join(data['keywords'])
85
+ writer.writerow([filename, title, keywords])
86
+
87
+ return [Image.open(img_file.name) for img_file in image_files], csv_file_path
88
+
89
+ # Define Gradio interface
90
+ iface = gr.Interface(
91
+ fn=process_images,
92
+ inputs=[
93
+ gr.Files(label="Upload Image Files", type="file", multiple=True)
94
+ ],
95
+ outputs=[
96
+ gr.Gallery(label="Processed Images"),
97
+ gr.File(label="Download Metadata CSV")
98
+ ],
99
+ title="Image Captioning and Metadata Generator",
100
+ description="Upload multiple images to generate captions and metadata. Download the metadata as a CSV file."
101
+ )
102
+
103
+ # Launch the interface
104
+ iface.launch(debug=True)