Repository navigation
Expand file tree
/
Copy pathgradio_app.py
More file actions
268 lines (222 loc) · 9.63 KB
/
Copy pathgradio_app.py
File metadata and controls
268 lines (222 loc) · 9.63 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import gradio as gr
import json
import os
import tempfile
from typing import Dict, Any, Optional
import traceback
# Import the existing skills
from extract_pdf_skill import process_pdf_from_url
from verify_tlsn_proof import verify_tlsn_proof, save_tlsn_proof
def handle_loan_application(
name: str,
loan_amount: float,
country: str,
bank: str,
has_income: bool,
email: str,
no_default_history: bool
) -> str:
"""
Handle the initial loan application form
"""
# Create a JSON with the loan application details
application_data = {
"first_name": name.split()[0] if len(name.split()) > 0 else "",
"last_name": name.split()[1] if len(name.split()) > 1 else "",
"loan_amount": loan_amount,
"country": country,
"bank": bank,
"income": "Yes" if has_income else "No",
"history": "No defaults" if no_default_history else "Has defaults",
"email": email
}
# Save to bank.json
with open("bank.json", "w") as f:
json.dump(application_data, f, indent=4)
return f"Application data saved successfully:\n\n{json.dumps(application_data, indent=4)}"
def handle_email_verification(raw_email: str) -> str:
"""
Handle the email verification step
"""
if not raw_email:
return "Please provide the raw email content."
# Save the raw email to a file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".eml")
try:
temp_file.write(raw_email.encode('utf-8'))
temp_file.close()
# Verify DKIM signature
result = verify_dkim_signature(temp_file.name)
# Try to extract relevant information from the email
# This is a placeholder - in a real implementation, you would extract more information
email_info = "Email processed successfully. DKIM verification results are shown in the console."
return email_info
except Exception as e:
return f"Error processing email: {str(e)}"
finally:
# Clean up
if os.path.exists(temp_file.name):
os.remove(temp_file.name)
def handle_email_upload(email_file) -> str:
"""
Handle uploaded email file
"""
if email_file is None:
return "No file uploaded"
try:
# Read the file content
with open(email_file.name, "r", encoding="utf-8", errors="ignore") as f:
email_content = f.read()
# Process the email
return handle_email_verification(email_content)
except Exception as e:
return f"Error processing email file: {str(e)}"
def handle_salary_verification(pdf_url: str) -> str:
"""
Handle the salary slip verification
"""
if not pdf_url:
return "Please provide a valid PDF URL."
try:
# Process the PDF
text = process_pdf_from_url(pdf_url)
if not text:
return "Failed to extract text from the PDF. Please check the URL and try again."
# Here we would extract information like account number, bank balance, etc.
# This is a simplified example
info_extract = "PDF processed successfully. Extracted text: " + text[:300] + "..."
# Try to update bank.json with additional information
try:
with open("bank.json", "r") as f:
bank_data = json.load(f)
# Here you would extract and add more data
bank_data["pdf_verified"] = True
with open("bank.json", "w") as f:
json.dump(bank_data, f, indent=4)
info_extract += "\n\nBank application data updated with PDF verification."
except Exception as e:
info_extract += f"\n\nWarning: Could not update application data: {str(e)}"
return info_extract
except Exception as e:
return f"Error processing PDF: {str(e)}"
def handle_tlsn_proof_text(proof_json: str) -> Dict[str, Any]:
"""
Handle TLSN proof verification from pasted JSON
"""
if not proof_json:
return {"success": False, "error": "Please provide the TLSN proof JSON."}
try:
# Save the proof to a file
save_result = save_tlsn_proof(proof_json)
if not save_result["success"]:
return save_result
# Verify the proof
result = verify_tlsn_proof(proof_json)
return result
except Exception as e:
traceback.print_exc() # Print the full exception traceback to console
return {"success": False, "error": f"Error processing TLSN proof: {str(e)}"}
def handle_tlsn_proof_upload(proof_file) -> Dict[str, Any]:
"""
Handle TLSN proof verification from uploaded file
"""
if proof_file is None:
return {"success": False, "error": "No file uploaded"}
try:
# Read the file
with open(proof_file.name, "r", encoding="utf-8") as f:
proof_json = f.read()
# Process the proof
return handle_tlsn_proof_text(proof_json)
except Exception as e:
traceback.print_exc() # Print the full exception traceback to console
return {"success": False, "error": f"Error processing TLSN proof file: {str(e)}"}
def create_ui():
"""
Create the Gradio UI
"""
with gr.Blocks(title="Lucid Loan Machine") as app:
gr.Markdown("# 💰 Lucid Loan Machine (LLM)")
gr.Markdown("## A secure loan application system powered by AI and zero-knowledge proofs")
with gr.Tab("Loan Application"):
gr.Markdown("### 📝 Complete this form to start your loan application")
with gr.Row():
with gr.Column():
name_input = gr.Textbox(label="Full Name")
loan_amount_input = gr.Number(label="Loan Amount")
country_input = gr.Textbox(label="Country")
bank_input = gr.Textbox(label="Bank")
has_income_input = gr.Checkbox(label="Do you have a job/proof of income?")
email_input = gr.Textbox(label="Email")
no_default_history_input = gr.Checkbox(label="No history of not paying back loans?")
submit_button = gr.Button("Submit Application")
with gr.Column():
application_output = gr.Textbox(label="Application Status", lines=10)
with gr.Tab("Email Verification"):
gr.Markdown("### 📧 Verify your identity with email verification")
with gr.Row():
with gr.Column():
gr.Markdown("#### Paste Raw Email")
raw_email_input = gr.Textbox(label="Raw Email Content", lines=15)
email_submit_button = gr.Button("Verify Email")
with gr.Column():
gr.Markdown("#### Or Upload Email File")
email_file_input = gr.File(label="Email File")
email_upload_button = gr.Button("Verify Uploaded Email")
email_verification_output = gr.Textbox(label="Email Verification Result", lines=10)
with gr.Tab("Salary Slip Verification"):
gr.Markdown("### 💼 Verify your income with a salary slip")
with gr.Row():
with gr.Column():
pdf_url_input = gr.Textbox(label="Salary Slip PDF URL")
pdf_submit_button = gr.Button("Verify Salary Slip")
with gr.Column():
salary_verification_output = gr.Textbox(label="Salary Verification Result", lines=10)
with gr.Tab("TLSN Proof Verification"):
gr.Markdown("### 🔐 Verify website data with TLSN proof")
with gr.Row():
with gr.Column():
gr.Markdown("#### Paste TLSN Proof JSON")
tlsn_proof_input = gr.Textbox(label="TLSN Proof JSON", lines=15)
tlsn_submit_button = gr.Button("Verify TLSN Proof")
with gr.Column():
gr.Markdown("#### Or Upload TLSN Proof File")
tlsn_file_input = gr.File(label="TLSN Proof File")
tlsn_upload_button = gr.Button("Verify Uploaded TLSN Proof")
tlsn_verification_output = gr.JSON(label="TLSN Verification Result")
# Set up event handlers
submit_button.click(
handle_loan_application,
inputs=[name_input, loan_amount_input, country_input, bank_input,
has_income_input, email_input, no_default_history_input],
outputs=application_output
)
email_submit_button.click(
handle_email_verification,
inputs=[raw_email_input],
outputs=email_verification_output
)
email_upload_button.click(
handle_email_upload,
inputs=[email_file_input],
outputs=email_verification_output
)
pdf_submit_button.click(
handle_salary_verification,
inputs=[pdf_url_input],
outputs=salary_verification_output
)
tlsn_submit_button.click(
handle_tlsn_proof_text,
inputs=[tlsn_proof_input],
outputs=tlsn_verification_output
)
tlsn_upload_button.click(
handle_tlsn_proof_upload,
inputs=[tlsn_file_input],
outputs=tlsn_verification_output
)
return app
if __name__ == "__main__":
app = create_ui()
app.launch()