Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import base64
|
| 3 |
+
import io
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
from flask_cors import CORS
|
| 7 |
+
from gradio_client import Client, handle_file
|
| 8 |
+
import openai
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
CORS(app)
|
| 12 |
+
|
| 13 |
+
# Initialize CLIP Interrogator client
|
| 14 |
+
clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
|
| 15 |
+
|
| 16 |
+
# Initialize LLM7 client
|
| 17 |
+
client = openai.OpenAI(
|
| 18 |
+
base_url="https://api.llm7.io/v1",
|
| 19 |
+
api_key=os.environ.get("LLM7_API_KEY", "unused") # Use a free key or environment variable
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
def get_image_description(image_path):
|
| 23 |
+
"""Get image description using CLIP Interrogator"""
|
| 24 |
+
try:
|
| 25 |
+
print("Calling CLIP Interrogator...")
|
| 26 |
+
result = clipi_client.predict(
|
| 27 |
+
image=handle_file(image_path),
|
| 28 |
+
mode="best",
|
| 29 |
+
best_max_flavors=4,
|
| 30 |
+
api_name="/clipi2"
|
| 31 |
+
)
|
| 32 |
+
print(f"CLIP description: {result}")
|
| 33 |
+
return result
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error in get_image_description: {e}")
|
| 36 |
+
return "a simple drawing"
|
| 37 |
+
|
| 38 |
+
def get_first_description(description):
|
| 39 |
+
"""Get only the first item from a comma-separated CLIP description"""
|
| 40 |
+
items = [item.strip() for item in description.split(",")]
|
| 41 |
+
return items[0] if items else description.strip()
|
| 42 |
+
|
| 43 |
+
def generate_story(description, audience="Children"):
|
| 44 |
+
"""Generate a kid-friendly story using GPT-5-Chat on LLM7 API"""
|
| 45 |
+
first_desc = get_first_description(description)
|
| 46 |
+
prompt = (
|
| 47 |
+
f"Create a short, kid-friendly story for {audience} about: {first_desc}. "
|
| 48 |
+
f"Use simple, cheerful words suitable for children. Include characters, action, "
|
| 49 |
+
f"and make it imaginative."
|
| 50 |
+
f"Write only 3 paragraphs."
|
| 51 |
+
f"Do NOT add extra questions, suggestions, or prompts at the end."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
print("Generating story with GPT-5-Chat...")
|
| 55 |
+
try:
|
| 56 |
+
response = client.chat.completions.create(
|
| 57 |
+
model="gpt-5-chat",
|
| 58 |
+
messages=[{"role": "user", "content": prompt}],
|
| 59 |
+
temperature=0.8
|
| 60 |
+
)
|
| 61 |
+
story = response.choices[0].message.content
|
| 62 |
+
return story
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"Error generating story: {e}")
|
| 65 |
+
return "Sorry, the story could not be generated."
|
| 66 |
+
|
| 67 |
+
@app.route("/health", methods=["GET"])
|
| 68 |
+
def health_check():
|
| 69 |
+
return jsonify({"status": "healthy", "message": "Image-to-Story API is running"})
|
| 70 |
+
|
| 71 |
+
@app.route("/generate-story-base64", methods=["POST"])
|
| 72 |
+
def generate_story_base64():
|
| 73 |
+
try:
|
| 74 |
+
data = request.get_json()
|
| 75 |
+
if "image" not in data:
|
| 76 |
+
return jsonify({"error": "No image provided"}), 400
|
| 77 |
+
|
| 78 |
+
# Decode base64
|
| 79 |
+
try:
|
| 80 |
+
image_data = base64.b64decode(data["image"])
|
| 81 |
+
image = Image.open(io.BytesIO(image_data))
|
| 82 |
+
except Exception:
|
| 83 |
+
return jsonify({"error": "Invalid image data"}), 400
|
| 84 |
+
|
| 85 |
+
if image.mode != "RGB":
|
| 86 |
+
image = image.convert("RGB")
|
| 87 |
+
|
| 88 |
+
temp_path = "temp_drawing.jpg"
|
| 89 |
+
image.save(temp_path, "JPEG")
|
| 90 |
+
|
| 91 |
+
audience = data.get("audience", "Children")
|
| 92 |
+
|
| 93 |
+
# Get description + generate story
|
| 94 |
+
description = get_image_description(temp_path)
|
| 95 |
+
story = generate_story(description, audience)
|
| 96 |
+
|
| 97 |
+
os.remove(temp_path)
|
| 98 |
+
|
| 99 |
+
return jsonify({
|
| 100 |
+
"success": True,
|
| 101 |
+
"description": description,
|
| 102 |
+
"story": story,
|
| 103 |
+
"audience": audience
|
| 104 |
+
})
|
| 105 |
+
|
| 106 |
+
except Exception as e:
|
| 107 |
+
return jsonify({"error": str(e)}), 500
|
| 108 |
+
|
| 109 |
+
def create_app():
|
| 110 |
+
return app
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
port = int(os.environ.get("PORT", 7860))
|
| 114 |
+
app.run(host="0.0.0.0", port=port, debug=False)
|