File size: 2,745 Bytes
3ac0c16
 
 
958c7d6
3ac0c16
 
 
 
 
 
 
 
 
 
 
cfbb031
958c7d6
 
3ac0c16
958c7d6
 
 
 
cfbb031
 
3ac0c16
 
958c7d6
cfbb031
3ac0c16
 
cfbb031
958c7d6
cfbb031
3ac0c16
cfbb031
958c7d6
cfbb031
3ac0c16
cfbb031
958c7d6
cfbb031
3ac0c16
958c7d6
 
cfbb031
3ac0c16
 
 
958c7d6
3ac0c16
 
 
 
 
 
cfbb031
958c7d6
cfbb031
 
 
3ac0c16
 
 
 
 
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
import os
import gradio as gr
import google.generativeai as genai
from PIL import Image

# Configure the Gemini API with environment variable
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if not GOOGLE_API_KEY:
    raise ValueError("GOOGLE_API_KEY environment variable not set. Please configure it in the Hugging Face Space settings.")

genai.configure(api_key=GOOGLE_API_KEY)

# Use Gemini 1.5 Flash model
model = genai.GenerativeModel('gemini-1.5-flash-latest')

def identify_banknote_denomination(image):
    if image is None:
        return "Error: Please upload or capture a banknote image."
    
    # Resize image for consistency (optional, Gemini handles various sizes)
    image = image.resize((224, 224))
    
    prompt = """
    You are an image analysis assistant tasked with identifying the denomination (worth) of a banknote from an image. 
    Analyze the image for visual elements like currency symbols, colors, numbers, or portraits to determine the denomination.
    Return the response in this format:
    
    Denomination: [Value, e.g., $10, €20]
    Reason: [Brief explanation]

    Examples:
    Image Description: A banknote with a green color scheme, a portrait of a historical figure, and "$100" printed prominently.
    Denomination: $100
    Reason: The green color scheme, portrait, and "$100" text are consistent with a U.S. $100 note.

    Image Description: A banknote with a blue color scheme, a bridge image, and "€50" printed.
    Denomination: €50
    Reason: The blue color scheme, bridge image, and "€50" text indicate a Euro €50 note.

    Image Description: A banknote with a purple color scheme, a specific building, and "£20" printed.
    Denomination: £20
    Reason: The purple color scheme, building image, and "£20" text are consistent with a British £20 note.

    Analyze the provided image.
    Denomination:
    Reason:
    """

    try:
        response = model.generate_content([prompt, image])
        return response.text.strip()
    except Exception as e:
        return f"Error: {str(e)}\nTip: Ensure your API key is valid at https://aistudio.google.com/"

# Define Gradio interface
iface = gr.Interface(
    fn=identify_banknote_denomination,
    inputs=gr.Image(type="pil", label="Upload or capture a banknote image", sources=["upload", "webcam"]),
    outputs=gr.Textbox(label="Banknote Denomination Result"),
    title="Banknote Denomination Predictor",
    description="Upload or capture a banknote image to identify its denomination using the Gemini API. Set your GOOGLE_API_KEY in the Hugging Face Space settings. Note: This is for demonstration purposes only and not for real-world banking use."
)

# Launch the interface
if __name__ == "__main__":
    iface.launch()