Spaces:
Sleeping
Sleeping
File size: 11,569 Bytes
23afe01 6f3fb70 23afe01 6f3fb70 23afe01 40aa7eb 23afe01 40aa7eb 23afe01 6f3fb70 23afe01 6f3fb70 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
import os
import imageio
import replicate
import gradio as gr
from dotenv import load_dotenv
from langfuse import Langfuse, observe, get_client
import numpy as np
import openai
import json
from pydantic import BaseModel, conint
from utils import encode_image
import asyncio
import logfire
from agents import (
Agent,
function_tool,
Runner,
WebSearchTool,
)
import tempfile
from functools import partial, update_wrapper
from visual_search_agent import seach_google_for_images
from agents import Agent, ItemHelpers, Runner, TResponseInputItem, trace
load_dotenv()
class ImageEvaluation(BaseModel):
score: conint(ge=1, le=10)
feedback: str
class HistoricalGrounding(BaseModel):
general_description: str
building_architecture: str
roads: str
transportation: str
people: str
people_clothing: str
logfire.configure(
service_name='my_agent_service',
send_to_logfire=False,
)
logfire.instrument_openai_agents()
langfuse = get_client()
twoBC_prompt = """Imagine how this image would look like in the 2nd BC."""
client = openai.OpenAI() # uses OPENAI_API_KEY
JUDGE_PROMPT = """
You are a historic critic.
You are provided with the description of scenes, a location and a year.
Your job is to judge how plausible the items describes belong that place at that era.
You must penalize items that are out-of-time.
Do not appraise the framing, the camera position or the camera technology.
You must rate this "truthfullness" on a scale of 1 to 10
and pricesely point out items that are out of time.
"""
@observe(name="image_captionning", capture_input=False, as_type="generation")
def image_caption(image, working_directory):
if isinstance(image, np.ndarray):
temp_image_path = os.path.join(working_directory, "temp_input_image.png")
imageio.imwrite(temp_image_path, image)
image_path = temp_image_path
else:
image_path = image
response = client.responses.create(
model="o4-mini-2025-04-16",
input=[{
"role": "user",
"content": [
{"type": "input_text", "text": "Describe this image, focusing on the human-maid items in the picture: buildings, roads, cloths,..."},
{
"type": "input_image",
"image_url": f"data:image/jpeg;base64,{encode_image(image_path)}",
},
],
}],
)
return response.output_text
@observe(name="llm_judge", capture_input=False, as_type="generation") # creates a span; captures inputs/outputs automatically
def judge_answer(image_description, location, year):
response = client.responses.parse(
model="gpt-4o-mini",
input=[
{
"role": "system",
"content": [
{"type": "input_text", "text": JUDGE_PROMPT},
],
},
{
"role": "user",
"content": [
{"type": "input_text", "text": f" image description : {image_description} . location : {location} . year : {year}"}
],
}],
text_format=ImageEvaluation
)
return json.loads(response.output_text)
@observe(name="image-generation", as_type="generation")
def generate_image(picture_design, input_image, working_directory):
"""
Calls the Replicate API to generate an image based on the input image.
Args:
prompt (str): The text prompt.
Returns:
str: Path to the generated image.
"""
# Gradio provides the image as a numpy array, but the replicate library expects a file path
# So we save the numpy array as a temporary image file
prompt = f"""
You are an expert photoshop user.
You are given a photo and you must transform it as to what it would have looked like at a certain time period.
You must apply the changes described in: {picture_design}
"""
if isinstance(input_image, np.ndarray):
temp_image_path = "temp_input_image.png"
imageio.imwrite(temp_image_path, input_image)
input_image = temp_image_path
with open(input_image, "rb") as image_file:
output = replicate.run(
"black-forest-labs/flux-kontext-pro",
input={
"prompt": prompt,
"input_image": image_file,
"aspect_ratio": "match_input_image",
"output_format": "jpg",
"safety_tolerance": 2,
"prompt_upsampling": False
}
)
num_images = len(os.listdir(working_directory))
output_image_path = os.path.join(working_directory, f"output_{num_images}.jpg")
print(f"Writing image in {output_image_path}")
with open(output_image_path, "wb") as f:
for chunk in output:
f.write(chunk)
return output_image_path
def create_rewind(image, text, date):
"""
Processes the inputs from Gradio and generates an image and text.
"""
prompt = f"{text} The scene is captured in the year {date}."
generated_image_path = generate_image(prompt)
output_text = f"This is the scene as it might have appeared in the year {date}."
return generated_image_path, output_text
@observe(name="historical_grounding", as_type="generation")
def get_historical_grounding(image_description, location, year):
instructions=f"""You are a historian. You are given the description of an image, a location and a time period.
You must reflect on what the scenary would look like at the period.
The nature of the scenary must remain unchanged : a seaside scenary remains a seaside scenary, a town center must remain a town center.
Be as historical accurate as possible about the items present in the image. Use the tools provided to search for images and look up information on internet.
For the visual description of the item, you can use the tools you are provided as well.
"""
response = client.responses.parse(
model="gpt-5-mini-2025-08-07",
input=[
{"role": "system", "content": instructions},
{
"role": "user",
"content": f"image description: {image_description}, location: {location}, year: {year}",
},
],
text_format=HistoricalGrounding,
tools=[{"type": "web_search_preview"}],
)
return response.output_text
def define_visual_cues_agent():
visual_cues_agent = Agent(
name="Visual Search Agent",
instructions="""You search for visual cues to illustrate specific items within a specific time period.
You provide a precise description of those items.
You can use the tools provided to search for images and look up information on internet et specific images.""",
model="gpt-5-mini-2025-08-07",
tools=[seach_google_for_images, WebSearchTool()],
)
return visual_cues_agent
def define_picture_designer_agent(image_path, working_directory):
picture_designer_agent = Agent(
name="Picture designer agent",
instructions="""
You are "picture designer" : you produce a text that will be used by an image generation tool
You receive as input the description of an image and a historical grounding of what that scene would look like at certain period of time.
Your goal is to modify the items visible on the image in such a way, that it would plausible that this image has been taken at period of time.
For instance, if the image is a picture of the Eiffel tower in Paris and the period is 3th BC, obviously the eiffel tower should be replaced by something else.
To help you in your task, you have access to the historical visual cue helper : this tool will provide you with precise descriptions of specific items, so that you can pricesely describe what to generate.
This text is to be interpreted by the image generation tool Replicate.
Some items in the description might be flagged as violent (butcher's clever), sexual (prostitutes), unsanitary (wastes)
Eliminate those possibly non-compliant items with the Replicate policy.
""",
model="o4-mini-2025-04-16",
tools=[WebSearchTool()],
handoffs=[define_visual_cues_agent()],
)
return picture_designer_agent
async def main(image_path, location, year) -> None:
working_directory = tempfile.mkdtemp("_scene_rewind")
print(f"Working in {working_directory}")
with trace("LLM as a judge"):
picture_description = image_caption(image_path, working_directory)
historical_grounding = get_historical_grounding(picture_description, location, year)
picture_designer_agent = define_picture_designer_agent(image_path, working_directory)
input_items: list[TResponseInputItem] = [
{
"content": f"Description:{picture_description}\n Historical grounding: {historical_grounding}",
"role": "user"
}]
latest_outline: str | None = None
while True:
picture_design = await Runner.run(
picture_designer_agent,
input_items,
)
input_items = picture_design.to_input_list()
latest_outline = ItemHelpers.text_message_outputs(picture_design.new_items)
print("Story outline generated")
try:
output_path = generate_image(latest_outline, image_path, working_directory)
created_image_caption = image_caption(output_path, working_directory)
judgment = judge_answer(created_image_caption, location, year)
except Exception:
judgment = {
"score": 0,
"feedback": "The image could not be produced as the content of the prompt as been flagged as sensitive by Replicate"
}
print(f"Evaluator score: {judgment['score']}")
if judgment["score"] > 6:
print("Story outline is good enough, exiting.")
break
print("Re-running with feedback")
input_items.append({"content": f"Feedback: {judgment['feedback']}", "role": "user"})
print(f"Final story outline: {latest_outline}")
return output_path, historical_grounding
# Create Gradio interface
def create_interface():
iface = gr.Interface(
fn=main, # Use wrapper function
inputs=[
gr.Image(type="numpy", label="Input Image"),
gr.Textbox(label="Location/Prompt", placeholder="e.g., Paris, Times Square, etc."),
gr.Slider(minimum=-2000, maximum=2000, value=1900, step=1, label="Target Year")
],
outputs=[
gr.Image(type="filepath", label="Historical Scene"),
gr.Textbox(label="Historical Description")
],
title="🕰️ Scene Rewind - Historical Time Travel",
description="Upload an image of a location, specify the place, and select a year to see how it might have looked in the past!",
examples=[
["images/paris.png", "Paris", 1700],
["images/newyork.jpg", "New York City", 1850],
] if False else None # Set to True if you have example images
)
return iface
if __name__ == "__main__":
# Option 1: Launch Gradio interface
interface = create_interface()
interface.launch(
share=False, # Set to True if you want a public link
server_name="127.0.0.1",
server_port=7860
) |