hashirlodhi commited on
Commit
3ac0c16
·
verified ·
1 Parent(s): 62b5469

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")
7
+ if not GOOGLE_API_KEY:
8
+ raise ValueError("GOOGLE_API_KEY environment variable not set. Please configure it in the Hugging Face Space settings.")
9
+
10
+ genai.configure(api_key=GOOGLE_API_KEY)
11
+
12
+ # Use Gemini 1.5 Flash model
13
+ model = genai.GenerativeModel('gemini-1.5-flash-latest')
14
+
15
+ def predict_banknote_authenticity(security_thread, watermark, microtext, ink_color_shift, paper_texture, printing_quality, holographic_features, serial_number, uv_features, edge_smoothness):
16
+ # Validate inputs
17
+ inputs = [security_thread, watermark, microtext, ink_color_shift, paper_texture, printing_quality, holographic_features, serial_number, uv_features, edge_smoothness]
18
+ if not all(inputs):
19
+ return "Error: Please provide answers to all questions."
20
+
21
+ # Format user responses
22
+ user_data = f"""
23
+ Security Thread: {security_thread}
24
+ Watermark: {watermark}
25
+ Microtext: {microtext}
26
+ Ink Color Shift: {ink_color_shift}
27
+ Paper Texture: {paper_texture}
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
+ User Data:
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-changing ink, crisp paper texture, sharp printing, holographic features, consistent serial numbers, correct UV glow, and smooth edges are strong indicators of an authentic banknote.
57
+
58
+ User Data:
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, and holographic features, along with rough paper, blurry printing, inconsistent serial numbers, incorrect UV response, and irregular edges, strongly suggest a counterfeit banknote.
71
+
72
+ User Data:
73
+ Security Thread: Visible but no color shift
74
+ Watermark: Faintly visible
75
+ Microtext: Partially legible
76
+ Ink Color Shift: No color change
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
+ User Data:
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=predict_banknote_authenticity,
101
+ inputs=[
102
+ gr.Dropdown(choices=["Visible and shifts color", "Visible but no color shift", "Not visible"], label="1. Is the security thread visible and does it shift color when tilted?"),
103
+ gr.Dropdown(choices=["Clear and visible when held to light", "Faintly visible", "Absent or blurry"], label="2. Is there a clear watermark visible when held to light?"),
104
+ gr.Dropdown(choices=["Legible under magnification", "Partially legible", "Illegible or absent"], label="3. Is microtext legible under magnification?"),
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
119
+ if __name__ == "__main__":
120
+ iface.launch()