Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from fastai.vision.all import *
|
| 4 |
from groq import Groq
|
| 5 |
from PIL import Image
|
|
@@ -13,10 +14,24 @@ client = Groq(
|
|
| 13 |
api_key=os.environ.get("GROQ_API_KEY"),
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def get_bird_info(bird_name):
|
| 17 |
"""Get detailed information about a bird using Groq API"""
|
|
|
|
|
|
|
| 18 |
prompt = f"""
|
| 19 |
-
Provide detailed information about the {
|
| 20 |
1. Physical characteristics and appearance
|
| 21 |
2. Habitat and distribution
|
| 22 |
3. Diet and behavior
|
|
@@ -36,7 +51,7 @@ def get_bird_info(bird_name):
|
|
| 36 |
"content": prompt,
|
| 37 |
}
|
| 38 |
],
|
| 39 |
-
model="
|
| 40 |
)
|
| 41 |
return chat_completion.choices[0].message.content
|
| 42 |
except Exception as e:
|
|
@@ -56,16 +71,18 @@ def predict_and_get_info(img):
|
|
| 56 |
top_probs = probs[top_indices]
|
| 57 |
top_labels = [labels[i] for i in top_indices]
|
| 58 |
|
| 59 |
-
# Format as dictionary for display
|
| 60 |
-
prediction_results = {top_labels[i]: float(top_probs[i]) for i in range(num_classes)}
|
| 61 |
|
| 62 |
-
# Get top prediction
|
| 63 |
top_bird = str(pred)
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Get detailed information about the top predicted bird
|
| 66 |
bird_info = get_bird_info(top_bird)
|
| 67 |
|
| 68 |
-
return prediction_results, bird_info,
|
| 69 |
|
| 70 |
def follow_up_question(question, bird_name):
|
| 71 |
"""Allow researchers to ask follow-up questions about the identified bird"""
|
|
@@ -77,6 +94,11 @@ def follow_up_question(question, bird_name):
|
|
| 77 |
|
| 78 |
Provide a detailed, scientific answer focusing on accurate ornithological information.
|
| 79 |
If the question relates to Tanzania or climate change impacts, emphasize those aspects in your response.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
Format your response in markdown for better readability.
|
| 81 |
"""
|
| 82 |
|
|
@@ -137,8 +159,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 137 |
return None, "Please upload an image", "", ""
|
| 138 |
|
| 139 |
try:
|
| 140 |
-
pred_results, info,
|
| 141 |
-
return pred_results, info,
|
| 142 |
except Exception as e:
|
| 143 |
return None, f"Error processing image: {str(e)}", "", ""
|
| 144 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import re
|
| 4 |
from fastai.vision.all import *
|
| 5 |
from groq import Groq
|
| 6 |
from PIL import Image
|
|
|
|
| 14 |
api_key=os.environ.get("GROQ_API_KEY"),
|
| 15 |
)
|
| 16 |
|
| 17 |
+
def clean_bird_name(name):
|
| 18 |
+
"""Clean bird name by removing numbers and special characters, and fix formatting"""
|
| 19 |
+
# Remove numbers and dots at the beginning
|
| 20 |
+
cleaned = re.sub(r'^\d+\.', '', name)
|
| 21 |
+
# Replace underscores with spaces
|
| 22 |
+
cleaned = cleaned.replace('_', ' ')
|
| 23 |
+
# Remove any remaining special characters
|
| 24 |
+
cleaned = re.sub(r'[^\w\s]', '', cleaned)
|
| 25 |
+
# Fix spacing
|
| 26 |
+
cleaned = ' '.join(cleaned.split())
|
| 27 |
+
return cleaned
|
| 28 |
+
|
| 29 |
def get_bird_info(bird_name):
|
| 30 |
"""Get detailed information about a bird using Groq API"""
|
| 31 |
+
clean_name = clean_bird_name(bird_name)
|
| 32 |
+
|
| 33 |
prompt = f"""
|
| 34 |
+
Provide detailed information about the {clean_name} bird, including:
|
| 35 |
1. Physical characteristics and appearance
|
| 36 |
2. Habitat and distribution
|
| 37 |
3. Diet and behavior
|
|
|
|
| 51 |
"content": prompt,
|
| 52 |
}
|
| 53 |
],
|
| 54 |
+
model="llama-3.3-70b-versatile",
|
| 55 |
)
|
| 56 |
return chat_completion.choices[0].message.content
|
| 57 |
except Exception as e:
|
|
|
|
| 71 |
top_probs = probs[top_indices]
|
| 72 |
top_labels = [labels[i] for i in top_indices]
|
| 73 |
|
| 74 |
+
# Format as dictionary with cleaned names for display
|
| 75 |
+
prediction_results = {clean_bird_name(top_labels[i]): float(top_probs[i]) for i in range(num_classes)}
|
| 76 |
|
| 77 |
+
# Get top prediction (original format for info retrieval)
|
| 78 |
top_bird = str(pred)
|
| 79 |
+
# Also keep a clean version for display
|
| 80 |
+
clean_top_bird = clean_bird_name(top_bird)
|
| 81 |
|
| 82 |
# Get detailed information about the top predicted bird
|
| 83 |
bird_info = get_bird_info(top_bird)
|
| 84 |
|
| 85 |
+
return prediction_results, bird_info, clean_top_bird
|
| 86 |
|
| 87 |
def follow_up_question(question, bird_name):
|
| 88 |
"""Allow researchers to ask follow-up questions about the identified bird"""
|
|
|
|
| 94 |
|
| 95 |
Provide a detailed, scientific answer focusing on accurate ornithological information.
|
| 96 |
If the question relates to Tanzania or climate change impacts, emphasize those aspects in your response.
|
| 97 |
+
|
| 98 |
+
IMPORTANT: Do not repeat basic introductory information about the bird that would have already been provided in a general description.
|
| 99 |
+
Do not start your answer with phrases like "Introduction to the {bird_name}" or similar repetitive headers.
|
| 100 |
+
Directly answer the specific question asked.
|
| 101 |
+
|
| 102 |
Format your response in markdown for better readability.
|
| 103 |
"""
|
| 104 |
|
|
|
|
| 159 |
return None, "Please upload an image", "", ""
|
| 160 |
|
| 161 |
try:
|
| 162 |
+
pred_results, info, clean_bird_name = predict_and_get_info(img)
|
| 163 |
+
return pred_results, info, clean_bird_name, ""
|
| 164 |
except Exception as e:
|
| 165 |
return None, f"Error processing image: {str(e)}", "", ""
|
| 166 |
|