|
import gradio as gr |
|
import openai |
|
import json |
|
from PIL import Image |
|
from google.oauth2 import service_account |
|
from baseline_utils import detect_text_in_image, summarize_diary_text, analyze_writer_image, generate_comic_book |
|
import glob |
|
import os |
|
|
|
|
|
openai_api_key = os.getenv("OPENAI_API_KEY") |
|
google_service_account_info = json.loads(os.getenv("GOOGLE_SERVICE_ACCOUNT")) |
|
gemini_api_key = os.getenv("GEMINI_API_KEY") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_google_credentials(): |
|
return service_account.Credentials.from_service_account_info(google_service_account_info) |
|
|
|
|
|
def process_images(diary_image, writer_image): |
|
|
|
diary_image_path = "temp_upload_images/temp_diary_image.png" |
|
writer_image_path = "temp_upload_images/temp_writer_image.png" |
|
os.makedirs("temp_upload_images", exist_ok=True) |
|
diary_image.save(diary_image_path) |
|
writer_image.save(writer_image_path) |
|
|
|
|
|
google_credentials = get_google_credentials() |
|
detected_text = detect_text_in_image(diary_image_path, google_credentials) |
|
summarized_text = summarize_diary_text(detected_text, openai_api_key) |
|
|
|
|
|
writer_summary = analyze_writer_image(writer_image_path, gemini_api_key) |
|
|
|
|
|
generate_comic_book(summarized_text, writer_summary, num_pages=4) |
|
|
|
|
|
image_files = sorted(glob.glob("comic_book/page_*.png")) |
|
|
|
return image_files |
|
|
|
|
|
|
|
def gradio_interface(diary_image, writer_image): |
|
|
|
generated_images = process_images(diary_image, writer_image) |
|
|
|
|
|
images = [Image.open(img) for img in generated_images] |
|
return images |
|
|
|
|
|
|
|
interface = gr.Interface( |
|
fn=gradio_interface, |
|
inputs=[ |
|
gr.Image(label="Upload your handwritten diary image", type="pil"), |
|
gr.Image(label="Upload a photo of the writer", type="pil"), |
|
], |
|
outputs=gr.Gallery(label="Generated Comic Book Pages"), |
|
title="Handwritten Diary to Comic Book" |
|
) |
|
|
|
|
|
interface.launch() |