jjz5463
commited on
Commit
Β·
cfda19a
1
Parent(s):
f13dda6
change back to gradio
Browse files- README.md +2 -2
- app.py +32 -29
- baseline_utils.py +1 -1
README.md
CHANGED
|
@@ -3,8 +3,8 @@ title: Diary-AI
|
|
| 3 |
emoji: π
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: blue
|
| 6 |
-
sdk:
|
| 7 |
-
sdk_version: "
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
| 3 |
emoji: π
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "4.44.0"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
import openai
|
| 3 |
import json
|
| 4 |
from PIL import Image
|
|
@@ -7,30 +7,19 @@ from baseline_utils import detect_text_in_image, summarize_diary_text, analyze_w
|
|
| 7 |
import glob
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
# Load secrets
|
| 11 |
-
openai_api_key =
|
| 12 |
-
google_service_account_info = json.loads(
|
| 13 |
-
gemini_api_key =
|
| 14 |
|
| 15 |
-
# Initialize OpenAI
|
| 16 |
-
openai.api_key = openai_api_key
|
| 17 |
|
| 18 |
# Function to get Google credentials
|
| 19 |
def get_google_credentials():
|
| 20 |
return service_account.Credentials.from_service_account_info(google_service_account_info)
|
| 21 |
|
| 22 |
-
st.title('Handwritten Diary to Comic Book')
|
| 23 |
-
uploaded_diary = st.file_uploader("Upload your handwritten diary image", type=["png", "jpg", "jpeg"])
|
| 24 |
-
uploaded_writer_image = st.file_uploader("Upload a photo of the writer", type=["png", "jpg", "jpeg"])
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# Read the uploaded images using file-like objects
|
| 30 |
-
diary_image = Image.open(uploaded_diary)
|
| 31 |
-
writer_image = Image.open(uploaded_writer_image)
|
| 32 |
-
|
| 33 |
-
# Save the file-like objects as image files (optional if needed)
|
| 34 |
diary_image_path = "temp_upload_images/temp_diary_image.png"
|
| 35 |
writer_image_path = "temp_upload_images/temp_writer_image.png"
|
| 36 |
os.makedirs("temp_upload_images", exist_ok=True)
|
|
@@ -41,25 +30,39 @@ if uploaded_diary and uploaded_writer_image:
|
|
| 41 |
google_credentials = get_google_credentials()
|
| 42 |
detected_text = detect_text_in_image(diary_image_path, google_credentials)
|
| 43 |
summarized_text = summarize_diary_text(detected_text, openai_api_key)
|
| 44 |
-
st.write(f"Summarized Diary Text: {summarized_text}")
|
| 45 |
|
| 46 |
# Analyze the writer's image using Gemini API
|
| 47 |
writer_summary = analyze_writer_image(writer_image_path, gemini_api_key)
|
| 48 |
-
st.write(f"Writer Description: {writer_summary}")
|
| 49 |
|
| 50 |
# Generate the comic book based on the summaries
|
| 51 |
-
st.write("Generating comic book images...")
|
| 52 |
generate_comic_book(summarized_text, writer_summary, num_pages=4)
|
| 53 |
|
| 54 |
-
st.write("Comic book generated successfully!")
|
| 55 |
-
|
| 56 |
# Assuming generated images are saved as 'comic_book/page_1.png', 'comic_book/page_2.png', etc.
|
| 57 |
image_files = sorted(glob.glob("comic_book/page_*.png")) # Find all the generated comic book pages
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Display each comic book page in the respective column
|
| 65 |
-
st.image(image_file, caption=image_file.split('/')[-1], use_column_width=True)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import json
|
| 4 |
from PIL import Image
|
|
|
|
| 7 |
import glob
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# Load secrets from Hugging Face Spaces environment
|
| 11 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
+
google_service_account_info = json.loads(os.getenv("GOOGLE_SERVICE_ACCOUNT"))
|
| 13 |
+
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
| 14 |
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Function to get Google credentials
|
| 17 |
def get_google_credentials():
|
| 18 |
return service_account.Credentials.from_service_account_info(google_service_account_info)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def process_images(diary_image, writer_image):
|
| 22 |
+
# Save the file-like objects as image files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
diary_image_path = "temp_upload_images/temp_diary_image.png"
|
| 24 |
writer_image_path = "temp_upload_images/temp_writer_image.png"
|
| 25 |
os.makedirs("temp_upload_images", exist_ok=True)
|
|
|
|
| 30 |
google_credentials = get_google_credentials()
|
| 31 |
detected_text = detect_text_in_image(diary_image_path, google_credentials)
|
| 32 |
summarized_text = summarize_diary_text(detected_text, openai_api_key)
|
|
|
|
| 33 |
|
| 34 |
# Analyze the writer's image using Gemini API
|
| 35 |
writer_summary = analyze_writer_image(writer_image_path, gemini_api_key)
|
|
|
|
| 36 |
|
| 37 |
# Generate the comic book based on the summaries
|
|
|
|
| 38 |
generate_comic_book(summarized_text, writer_summary, num_pages=4)
|
| 39 |
|
|
|
|
|
|
|
| 40 |
# Assuming generated images are saved as 'comic_book/page_1.png', 'comic_book/page_2.png', etc.
|
| 41 |
image_files = sorted(glob.glob("comic_book/page_*.png")) # Find all the generated comic book pages
|
| 42 |
|
| 43 |
+
return image_files
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Define the Gradio interface
|
| 47 |
+
def gradio_interface(diary_image, writer_image):
|
| 48 |
+
# Process the images and generate comic book pages
|
| 49 |
+
generated_images = process_images(diary_image, writer_image)
|
| 50 |
+
|
| 51 |
+
# Load the images and return them
|
| 52 |
+
images = [Image.open(img) for img in generated_images]
|
| 53 |
+
return images
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Set up the Gradio interface
|
| 57 |
+
interface = gr.Interface(
|
| 58 |
+
fn=gradio_interface,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.Image(label="Upload your handwritten diary image", type="pil"),
|
| 61 |
+
gr.Image(label="Upload a photo of the writer", type="pil"),
|
| 62 |
+
],
|
| 63 |
+
outputs=gr.Gallery(label="Generated Comic Book Pages"),
|
| 64 |
+
title="Handwritten Diary to Comic Book"
|
| 65 |
+
)
|
| 66 |
|
| 67 |
+
# Launch the interface
|
| 68 |
+
interface.launch()
|
|
|
|
|
|
baseline_utils.py
CHANGED
|
@@ -79,7 +79,7 @@ def generate_comic_book(diary_text, writer_description, num_pages=4):
|
|
| 79 |
"stabilityai/sdxl-turbo",
|
| 80 |
torch_dtype=torch.float16,
|
| 81 |
variant="fp16",
|
| 82 |
-
cache_dir="./SDXL-Turbo"
|
| 83 |
)
|
| 84 |
|
| 85 |
# Check for available device: CUDA, MPS, or CPU
|
|
|
|
| 79 |
"stabilityai/sdxl-turbo",
|
| 80 |
torch_dtype=torch.float16,
|
| 81 |
variant="fp16",
|
| 82 |
+
#cache_dir="./SDXL-Turbo"
|
| 83 |
)
|
| 84 |
|
| 85 |
# Check for available device: CUDA, MPS, or CPU
|