Phauglin commited on
Commit
69de931
·
verified ·
1 Parent(s): 9cca2b0

implement ai generation and display it

Browse files
Files changed (1) hide show
  1. app.py +82 -20
app.py CHANGED
@@ -1,34 +1,96 @@
1
  from fastai.vision.all import *
2
  import gradio as gr
3
  import fal_client
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def on_queue_update(update):
6
  if isinstance(update, fal_client.InProgress):
7
  for log in update.logs:
8
  print(log["message"])
9
 
10
-
11
- def classify_image(img):
12
  pred, idx, probs = learn.predict(img)
13
- return dict(zip(categories, map(float, probs)))
14
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
 
16
  learn = load_learner('export.pkl')
17
 
18
- categories = ('balsamroot', 'bladderpod', 'blazing star', 'bristlecone pine flowers', 'brittlebrush')
19
-
20
- image=gr.Image(height = 192, width = 192)
21
- label = gr.Label()
22
- examples = ['https://www.deserthorizonnursery.com/wp-content/uploads/2024/03/Brittlebush-Encelia-Farinosa-desert-horizon-nursery.jpg','https://cdn.mos.cms.futurecdn.net/VJE7gSuQ9KWbkqEsWgX5zS.jpg']
23
- intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
24
- intf.launch(inline=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- result = fal_client.subscribe(
27
- "fal-ai/flux/schnell",
28
- arguments={
29
- "prompt": "Extreme close-up of a single tiger eye, direct frontal view. Detailed iris and pupil. Sharp focus on eye texture and color. Natural lighting to capture authentic eye shine and depth. The word \"FLUX\" is painted over it in big, white brush strokes with visible texture."
30
- },
31
- with_logs=True,
32
- on_queue_update=on_queue_update,
33
- )
34
- print(result)
 
1
  from fastai.vision.all import *
2
  import gradio as gr
3
  import fal_client
4
+ from PIL import Image
5
+ import io
6
+ import base64
7
+
8
+ search_terms_wikipedia = {
9
+ "blazing star": "https://en.wikipedia.org/wiki/Mentzelia",
10
+ "bristlecone pine": "https://en.wikipedia.org/wiki/Pinus_longaeva",
11
+ "california bluebell": "https://en.wikipedia.org/wiki/Phacelia_minor",
12
+ "california buckeye": "https://en.wikipedia.org/wiki/Aesculus_californica",
13
+ "california buckwheat": "https://en.wikipedia.org/wiki/Eriogonum_fasciculatum",
14
+ "california fuchsia": "https://en.wikipedia.org/wiki/Epilobium_canum",
15
+ "california checkerbloom": "https://en.wikipedia.org/wiki/Sidalcea_malviflora",
16
+ "california lilac": "https://en.wikipedia.org/wiki/Ceanothus",
17
+ "california poppy": "https://en.wikipedia.org/wiki/Eschscholzia_californica",
18
+ "california sagebrush": "https://en.wikipedia.org/wiki/Artemisia_californica",
19
+ "california wild grape": "https://en.wikipedia.org/wiki/Vitis_californica",
20
+ "california wild rose": "https://en.wikipedia.org/wiki/Rosa_californica",
21
+ "coyote mint": "https://en.wikipedia.org/wiki/Monardella",
22
+ "elegant clarkia": "https://en.wikipedia.org/wiki/Clarkia_unguiculata",
23
+ "baby blue eyes": "https://en.wikipedia.org/wiki/Nemophila_menziesii",
24
+ "hummingbird sage": "https://en.wikipedia.org/wiki/Salvia_spathacea",
25
+ "delphiniumr": "https://en.wikipedia.org/wiki/Delphinium",
26
+ "matilija poppy": "https://en.wikipedia.org/wiki/Romneya_coulteri",
27
+ "blue-eyed grass": "https://en.wikipedia.org/wiki/Sisyrinchium_bellum",
28
+ "penstemon spectabilis": "https://en.wikipedia.org/wiki/Penstemon_spectabilis",
29
+ "seaside daisy": "https://en.wikipedia.org/wiki/Erigeron_glaucus",
30
+ "sticky monkeyflower": "https://en.wikipedia.org/wiki/Diplacus_aurantiacus",
31
+ "tidy tips": "https://en.wikipedia.org/wiki/Layia_platyglossa",
32
+ "wild cucumber": "https://en.wikipedia.org/wiki/Marah_(plant)",
33
+ "douglas iris": "https://en.wikipedia.org/wiki/Iris_douglasiana",
34
+ "goldfields coreopsis": "https://en.wikipedia.org/wiki/Coreopsis"
35
+ }
36
 
37
  def on_queue_update(update):
38
  if isinstance(update, fal_client.InProgress):
39
  for log in update.logs:
40
  print(log["message"])
41
 
42
+ def process_image(img):
43
+ # First do the classification
44
  pred, idx, probs = learn.predict(img)
45
+ classification_results = dict(zip(search_terms_wikipedia.keys(), map(float, probs)))
46
+
47
+ # Get Wikipedia URL for the predicted class
48
+ predicted_class = max(classification_results.items(), key=lambda x: x[1])[0]
49
+ wiki_url = search_terms_wikipedia.get(predicted_class, "No Wikipedia entry found.")
50
+
51
+ # Generate FLUX image
52
+ with gr.Status("Generating AI image based on classification..."):
53
+ result = fal_client.subscribe(
54
+ "fal-ai/flux/schnell",
55
+ arguments={
56
+ "prompt": f"A detailed, artistic interpretation of {predicted_class} flower in natural setting",
57
+ "image_size": "256x256" # Low res for testing
58
+ },
59
+ with_logs=True,
60
+ on_queue_update=on_queue_update,
61
+ )
62
+
63
+ # Convert the image data
64
+ image_data = base64.b64decode(result['image'])
65
+ generated_image = Image.open(io.BytesIO(image_data))
66
+
67
+ return classification_results, generated_image, wiki_url
68
 
69
+ # Load the learner
70
  learn = load_learner('export.pkl')
71
 
72
+ # Create Gradio interface
73
+ with gr.Blocks() as demo:
74
+ with gr.Row():
75
+ input_image = gr.Image(height=192, width=192, label="Upload Image for Classification")
76
+ with gr.Row():
77
+ with gr.Column():
78
+ label_output = gr.Label(label="Classification Results")
79
+ wiki_output = gr.Textbox(label="Wikipedia Article Link", lines=1)
80
+ generated_image = gr.Image(label="AI Generated Interpretation")
81
+
82
+ # Example images
83
+ examples = [
84
+ 'https://www.deserthorizonnursery.com/wp-content/uploads/2024/03/Brittlebush-Encelia-Farinosa-desert-horizon-nursery.jpg',
85
+ 'https://cdn.mos.cms.futurecdn.net/VJE7gSuQ9KWbkqEsWgX5zS.jpg'
86
+ ]
87
+ gr.Examples(examples, input_image)
88
+
89
+ # Set up event handler
90
+ input_image.change(
91
+ fn=process_image,
92
+ inputs=input_image,
93
+ outputs=[label_output, generated_image, wiki_output]
94
+ )
95
 
96
+ demo.launch(inline=False)