Sazzz02 commited on
Commit
abe60f3
·
verified ·
1 Parent(s): bfac87d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -23
app.py CHANGED
@@ -3,22 +3,18 @@ import pandas as pd
3
  from transformers import pipeline
4
  from PIL import Image
5
  import requests
6
- import os
7
 
8
- # ---- SETUP SECTION ----
 
 
9
 
10
- # Load your landmarks dataset
11
- # Place "gldv2_info.csv" in the root of your Space
12
- df = pd.read_csv("gldv2_info.csv") # columns: gldv2_id, url, landmark_id, name
13
-
14
- # Text/story generator: use a free/fast mythology model from HF
15
  try:
16
  story_pipe = pipeline("text-generation", model="Samurai719214/gptneo-mythology-storyteller")
17
  except Exception:
18
- # Fallback model if main one fails
19
  story_pipe = pipeline("text-generation", model="mahing/historical-narrative-generator")
20
 
21
- # Image generator: Stable Diffusion (for cases where no matching image is found)
22
  try:
23
  from diffusers import StableDiffusionPipeline
24
  import torch
@@ -30,12 +26,12 @@ try:
30
  except Exception:
31
  sd_pipe = None
32
 
33
- # ---- APP LOGIC ----
34
-
35
  def landmark_explorer(site_name):
36
- # 1. Find matching images
37
- matches = df[df['name'].str.strip().str.lower() == site_name.strip().lower()]
38
  images = []
 
39
  for url in matches['url']:
40
  try:
41
  img = Image.open(requests.get(url, stream=True, timeout=6).raw).convert("RGB")
@@ -43,7 +39,7 @@ def landmark_explorer(site_name):
43
  except Exception:
44
  continue
45
 
46
- # 2. Generate story
47
  story_prompt = (
48
  f"Tell a mythological and historical story about the site: {site_name} in Egypt. "
49
  "Include both legend and real archaeological facts where possible."
@@ -54,11 +50,10 @@ def landmark_explorer(site_name):
54
  except Exception:
55
  story = "Unable to generate story for this landmark at the moment."
56
 
57
- # 3. If no images, generate a relevant one (optional)
58
  if not images and sd_pipe is not None:
59
  try:
60
- sd_out = sd_pipe(f"{site_name} in ancient Egypt, detailed, realistic, landscape")[0]
61
- images = [sd_out]
62
  except Exception:
63
  pass
64
 
@@ -66,8 +61,7 @@ def landmark_explorer(site_name):
66
  return story, None
67
  return story, images
68
 
69
- # ---- GRADIO INTERFACE ----
70
-
71
  with gr.Blocks() as demo:
72
  gr.Markdown(
73
  """
@@ -77,7 +71,7 @@ with gr.Blocks() as demo:
77
  )
78
  name_input = gr.Textbox(label="Enter Landmark Name (e.g., Great_Pyramid_of_Giza)")
79
  story_output = gr.Textbox(label="Generated Story")
80
- gallery_output = gr.Gallery(label="Images").style(grid=3)
81
  run_btn = gr.Button("Explore")
82
 
83
  run_btn.click(
@@ -87,10 +81,8 @@ with gr.Blocks() as demo:
87
  )
88
 
89
  gr.Markdown(
90
- "Sample names: Great_Pyramid_of_Giza, Karnak, Temple_of_Edfu, Bab_al-Nasr_(Cairo), etc."
91
  )
92
 
93
- # ---- LAUNCH APP ----
94
-
95
  if __name__ == "__main__":
96
  demo.launch()
 
3
  from transformers import pipeline
4
  from PIL import Image
5
  import requests
 
6
 
7
+ # ---- SETUP ----
8
+ # Load dataset (make sure 'gldv2_info.csv' is in the project root)
9
+ df = pd.read_csv("gldv2_info.csv")
10
 
11
+ # Load a free mythology/historical text generator from Hugging Face
 
 
 
 
12
  try:
13
  story_pipe = pipeline("text-generation", model="Samurai719214/gptneo-mythology-storyteller")
14
  except Exception:
 
15
  story_pipe = pipeline("text-generation", model="mahing/historical-narrative-generator")
16
 
17
+ # Optional: set up text-to-image only if 'diffusers' is available and desired
18
  try:
19
  from diffusers import StableDiffusionPipeline
20
  import torch
 
26
  except Exception:
27
  sd_pipe = None
28
 
29
+ # ---- APP FUNCTION ----
 
30
  def landmark_explorer(site_name):
31
+ site_name_clean = site_name.strip().lower()
32
+ matches = df[df['name'].str.strip().str.lower() == site_name_clean]
33
  images = []
34
+ # Gather all valid images
35
  for url in matches['url']:
36
  try:
37
  img = Image.open(requests.get(url, stream=True, timeout=6).raw).convert("RGB")
 
39
  except Exception:
40
  continue
41
 
42
+ # Generate historical/myth story
43
  story_prompt = (
44
  f"Tell a mythological and historical story about the site: {site_name} in Egypt. "
45
  "Include both legend and real archaeological facts where possible."
 
50
  except Exception:
51
  story = "Unable to generate story for this landmark at the moment."
52
 
53
+ # If no images, generate one with Stable Diffusion (optional)
54
  if not images and sd_pipe is not None:
55
  try:
56
+ images = [sd_pipe(f"{site_name} in ancient Egypt, detailed, realistic, landscape").images[0]]
 
57
  except Exception:
58
  pass
59
 
 
61
  return story, None
62
  return story, images
63
 
64
+ # ---- GRADIO UI ----
 
65
  with gr.Blocks() as demo:
66
  gr.Markdown(
67
  """
 
71
  )
72
  name_input = gr.Textbox(label="Enter Landmark Name (e.g., Great_Pyramid_of_Giza)")
73
  story_output = gr.Textbox(label="Generated Story")
74
+ gallery_output = gr.Gallery(label="Images", columns=3)
75
  run_btn = gr.Button("Explore")
76
 
77
  run_btn.click(
 
81
  )
82
 
83
  gr.Markdown(
84
+ "Sample landmark names: Great_Pyramid_of_Giza, Karnak, Temple_of_Edfu, Bab_al-Nasr_(Cairo), etc."
85
  )
86
 
 
 
87
  if __name__ == "__main__":
88
  demo.launch()