Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
|
|
|
| 4 |
|
| 5 |
# Configure the Gemini API with environment variable
|
| 6 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
|
@@ -12,107 +13,63 @@ genai.configure(api_key=GOOGLE_API_KEY)
|
|
| 12 |
# Use Gemini 1.5 Flash model
|
| 13 |
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 14 |
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if not all(inputs):
|
| 19 |
-
return "Error: Please provide answers to all questions."
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
Printing Quality: {printing_quality}
|
| 29 |
-
Holographic Features: {holographic_features}
|
| 30 |
-
Serial Number Consistency: {serial_number}
|
| 31 |
-
UV Features: {uv_features}
|
| 32 |
-
Edge Smoothness: {edge_smoothness}
|
| 33 |
-
"""
|
| 34 |
-
|
| 35 |
-
prompt = f"""
|
| 36 |
-
You are an assistant predicting whether a banknote is authentic or counterfeit based on user responses to 10 questions about its physical and visual characteristics.
|
| 37 |
-
Classify the banknote as "Authentic" or "Fake" and provide a brief reason for your classification.
|
| 38 |
Return the response in this format:
|
| 39 |
|
| 40 |
Prediction: [Authentic / Fake]
|
| 41 |
-
Reason: [Brief explanation]
|
|
|
|
|
|
|
| 42 |
|
| 43 |
Examples:
|
| 44 |
-
|
| 45 |
-
Security Thread: Visible and shifts color
|
| 46 |
-
Watermark: Clear and visible when held to light
|
| 47 |
-
Microtext: Legible under magnification
|
| 48 |
-
Ink Color Shift: Changes color when tilted
|
| 49 |
-
Paper Texture: Crisp and unique
|
| 50 |
-
Printing Quality: Sharp and clear
|
| 51 |
-
Holographic Features: Present and changes with angle
|
| 52 |
-
Serial Number Consistency: Uniform and aligned
|
| 53 |
-
UV Features: Glows correctly under UV light
|
| 54 |
-
Edge Smoothness: Smooth and precise
|
| 55 |
Prediction: Authentic
|
| 56 |
-
Reason: The presence of a color-shifting security thread, clear watermark, legible microtext, color-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
Security Thread: Not visible
|
| 60 |
-
Watermark: Absent or blurry
|
| 61 |
-
Microtext: Illegible or absent
|
| 62 |
-
Ink Color Shift: No color change
|
| 63 |
-
Paper Texture: Rough or ordinary
|
| 64 |
-
Printing Quality: Blurry or uneven
|
| 65 |
-
Holographic Features: Absent
|
| 66 |
-
Serial Number Consistency: Misaligned or inconsistent
|
| 67 |
-
UV Features: No glow or incorrect glow
|
| 68 |
-
Edge Smoothness: Rough or irregular
|
| 69 |
Prediction: Fake
|
| 70 |
-
Reason: The absence of a security thread, watermark, microtext, color-shifting ink,
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
Paper Texture: Crisp and unique
|
| 78 |
-
Printing Quality: Sharp and clear
|
| 79 |
-
Holographic Features: Absent
|
| 80 |
-
Serial Number Consistency: Uniform and aligned
|
| 81 |
-
UV Features: Glows correctly under UV light
|
| 82 |
-
Edge Smoothness: Smooth and precise
|
| 83 |
-
Prediction: Fake
|
| 84 |
-
Reason: While some features like paper texture, printing quality, serial number consistency, UV glow, and edge smoothness are present, the lack of a color-shifting security thread, faint watermark, partially legible microtext, absent holographic features, and no color-shifting ink suggest a counterfeit banknote.
|
| 85 |
|
| 86 |
-
|
| 87 |
-
{user_data}
|
| 88 |
Prediction:
|
| 89 |
-
Reason:
|
|
|
|
|
|
|
| 90 |
"""
|
| 91 |
|
| 92 |
try:
|
| 93 |
-
response = model.generate_content(prompt)
|
| 94 |
return response.text.strip()
|
| 95 |
except Exception as e:
|
| 96 |
return f"Error: {str(e)}\nTip: Ensure your API key is valid at https://aistudio.google.com/"
|
| 97 |
|
| 98 |
# Define Gradio interface
|
| 99 |
iface = gr.Interface(
|
| 100 |
-
fn=
|
| 101 |
-
inputs=[
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
gr.Dropdown(choices=["Changes color when tilted", "No color change"], label="4. Does the ink in specific areas (e.g., denomination) change color when tilted?"),
|
| 106 |
-
gr.Dropdown(choices=["Crisp and unique", "Rough or ordinary"], label="5. Does the paper have a crisp, unique texture?"),
|
| 107 |
-
gr.Dropdown(choices=["Sharp and clear", "Blurry or uneven"], label="6. Is the printing quality sharp and clear?"),
|
| 108 |
-
gr.Dropdown(choices=["Present and changes with angle", "Absent"], label="7. Are holographic features present and do they change with angle?"),
|
| 109 |
-
gr.Dropdown(choices=["Uniform and aligned", "Misaligned or inconsistent"], label="8. Are the serial numbers uniform and aligned?"),
|
| 110 |
-
gr.Dropdown(choices=["Glows correctly under UV light", "No glow or incorrect glow"], label="9. Do specific elements glow correctly under UV light?"),
|
| 111 |
-
gr.Dropdown(choices=["Smooth and precise", "Rough or irregular"], label="10. Are the edges of the note smooth and precise?")
|
| 112 |
-
],
|
| 113 |
-
outputs=gr.Textbox(label="Banknote Authentication Prediction"),
|
| 114 |
-
title="Banknote Authenticity Predictor",
|
| 115 |
-
description="Answer 10 questions about a banknote's characteristics to predict whether it is authentic or counterfeit 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."
|
| 116 |
)
|
| 117 |
|
| 118 |
# Launch the interface
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
# Configure the Gemini API with environment variable
|
| 7 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
|
|
|
| 13 |
# Use Gemini 1.5 Flash model
|
| 14 |
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 15 |
|
| 16 |
+
def authenticate_banknote(image):
|
| 17 |
+
if image is None:
|
| 18 |
+
return "Error: Please upload or capture a banknote image."
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Resize image for consistency (optional, Gemini handles various sizes)
|
| 21 |
+
image = image.resize((224, 224))
|
| 22 |
+
|
| 23 |
+
prompt = """
|
| 24 |
+
You are an image analysis assistant tasked with determining whether a banknote image is authentic or counterfeit and identifying its denomination (worth).
|
| 25 |
+
Analyze the image for security features (e.g., security thread, watermark, microtext, color-shifting ink, holograms, printing quality) to determine authenticity.
|
| 26 |
+
Identify the denomination based on visual elements like currency symbols, colors, numbers, or portraits.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
Return the response in this format:
|
| 28 |
|
| 29 |
Prediction: [Authentic / Fake]
|
| 30 |
+
Reason for Authenticity: [Brief explanation]
|
| 31 |
+
Denomination: [Value, e.g., $10, €20]
|
| 32 |
+
Reason for Denomination: [Brief explanation]
|
| 33 |
|
| 34 |
Examples:
|
| 35 |
+
Image Description: A banknote with a visible security thread that shifts color, a clear watermark visible when held to light, legible microtext under magnification, color-shifting ink, crisp paper texture, sharp printing, and a holographic feature that changes with angle. The note has a green color scheme and a portrait of a historical figure with "$100" printed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
Prediction: Authentic
|
| 37 |
+
Reason for Authenticity: The presence of a color-shifting security thread, clear watermark, legible microtext, color-shifting ink, crisp paper, sharp printing, and holographic features are strong indicators of an authentic banknote.
|
| 38 |
+
Denomination: $100
|
| 39 |
+
Reason for Denomination: The green color scheme, portrait, and "$100" text are consistent with a U.S. $100 note.
|
| 40 |
|
| 41 |
+
Image Description: A banknote with no visible security thread, a blurry watermark, illegible microtext, no color-shifting ink, rough paper texture, and blurry printing. The note has a blue color scheme and "€50" printed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
Prediction: Fake
|
| 43 |
+
Reason for Authenticity: The absence of a security thread, blurry watermark, illegible microtext, lack of color-shifting ink, rough paper, and blurry printing suggest a counterfeit banknote.
|
| 44 |
+
Denomination: €50
|
| 45 |
+
Reason for Denomination: The blue color scheme and "€50" text indicate a Euro €50 note, despite its counterfeit nature.
|
| 46 |
|
| 47 |
+
Image Description: A banknote with a security thread, clear watermark, legible microtext, and color-shifting ink. The note has a purple color scheme and "£20" printed.
|
| 48 |
+
Prediction: Authentic
|
| 49 |
+
Reason for Authenticity: The presence of a security thread, clear watermark, legible microtext, and color-shifting ink are consistent with an authentic banknote.
|
| 50 |
+
Denomination: £20
|
| 51 |
+
Reason for Denomination: The purple color scheme and "£20" text are consistent with a British £20 note.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
Analyze the provided image.
|
|
|
|
| 54 |
Prediction:
|
| 55 |
+
Reason for Authenticity:
|
| 56 |
+
Denomination:
|
| 57 |
+
Reason for Denomination:
|
| 58 |
"""
|
| 59 |
|
| 60 |
try:
|
| 61 |
+
response = model.generate_content([prompt, image])
|
| 62 |
return response.text.strip()
|
| 63 |
except Exception as e:
|
| 64 |
return f"Error: {str(e)}\nTip: Ensure your API key is valid at https://aistudio.google.com/"
|
| 65 |
|
| 66 |
# Define Gradio interface
|
| 67 |
iface = gr.Interface(
|
| 68 |
+
fn=authenticate_banknote,
|
| 69 |
+
inputs=gr.Image(type="pil", label="Upload or capture a banknote image", sources=["upload", "webcam"]),
|
| 70 |
+
outputs=gr.Textbox(label="Banknote Authentication and Denomination Result"),
|
| 71 |
+
title="Banknote Authenticity and Denomination Predictor",
|
| 72 |
+
description="Upload or capture a banknote image to determine if it is authentic or counterfeit and 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."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
# Launch the interface
|