-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegrated_app.py
More file actions
267 lines (221 loc) · 9.91 KB
/
integrated_app.py
File metadata and controls
267 lines (221 loc) · 9.91 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
import autogen
import os
import json
import gradio as gr
from dotenv import load_dotenv
from typing import Dict, Any, Optional
# Import existing components
from main import front_desk_assistant, email_assistant, salary_slip_assistant
from system_prompts import front_desk_assistant_prompt, email_assistant_prompt, salary_slip_assistant_prompt, verify_tlsn_proof_prompt
from extract_pdf_skill import process_pdf_from_url
from verify_tlsn_proof import verify_tlsn_proof, save_tlsn_proof
load_dotenv() # Take environment variables from .env
# Set up Autogen config
config_list = [
{
'model': 'gpt-3.5-turbo',
'api_key': os.getenv('OPENAI_API_KEY'),
}
]
llm_config = {
"timeout": 120,
"seed": 42,
"config_list": config_list,
"temperature": 0
}
class AutogenIntegration:
"""
Class to integrate Gradio UI with Autogen agents
"""
def __init__(self):
# Initialize the assistants
self.front_desk_assistant = autogen.AssistantAgent(
name="front_desk_assistant",
llm_config=llm_config,
system_message=front_desk_assistant_prompt,
)
self.email_assistant = autogen.AssistantAgent(
name="email_assistant",
llm_config=llm_config,
system_message=email_assistant_prompt
)
self.salary_slip_assistant = autogen.AssistantAgent(
name="salary_slip_assistant",
llm_config=llm_config,
system_message=salary_slip_assistant_prompt
)
self.tlsn_assistant = autogen.AssistantAgent(
name="tlsn_assistant",
llm_config=llm_config,
system_message=verify_tlsn_proof_prompt
)
self.user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER", # Automated for Gradio integration
code_execution_config={
"work_dir": "code",
"use_docker": False,
}
)
# Register functions for the assistants
self.register_assistant_functions()
def register_assistant_functions(self):
"""Register functions for all assistants"""
# Register functions for email assistant
self.email_assistant.register_function(
function_map={
"process_email": self.process_email,
}
)
# Register functions for salary slip assistant
self.salary_slip_assistant.register_function(
function_map={
"process_pdf_from_url": process_pdf_from_url,
}
)
# Register functions for TLSN assistant
self.tlsn_assistant.register_function(
function_map={
"verify_tlsn_proof": verify_tlsn_proof,
"save_tlsn_proof": save_tlsn_proof,
}
)
def process_email(self, raw_email: str) -> Dict[str, Any]:
"""Process raw email and return results"""
try:
# Save email to file
with open("raw_email.txt", "w") as f:
f.write(raw_email)
# For demonstration, return basic info
return {
"success": True,
"message": "Email processed and saved for verification"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def handle_loan_application(self, name, loan_amount, country, bank, has_income, email, no_default_history):
"""Handle loan application through front desk assistant"""
# Prepare message
message = f"""
I want to apply for a loan. Here's my information:
Name: {name}
Loan Amount: {loan_amount}
Country: {country}
Bank: {bank}
Do I have income: {'Yes' if has_income else 'No'}
Email: {email}
No history of defaults: {'Yes' if no_default_history else 'No'}
"""
# Interact with front desk assistant
self.user_proxy.initiate_chat(self.front_desk_assistant, message=message)
# Return confirmation
try:
with open("bank.json", "r") as f:
data = json.load(f)
return f"Application processed successfully. Your data has been saved:\n\n{json.dumps(data, indent=2)}"
except:
return "Application processed. Please check the console for details."
def handle_email_verification(self, raw_email):
"""Handle email verification through email assistant"""
# Prepare message
message = f"Here's my raw email for verification: [Email begins]\n{raw_email[:500]}...[Email trimmed]"
# Interact with email assistant
self.user_proxy.initiate_chat(self.email_assistant, message=message)
# Save email for processing
self.process_email(raw_email)
return "Email received for verification. Please check the console for verification details."
def handle_salary_verification(self, pdf_url):
"""Handle salary slip verification through salary slip assistant"""
# Prepare message
message = f"Here's the URL to my salary slip: {pdf_url}"
# Interact with salary slip assistant
self.user_proxy.initiate_chat(self.salary_slip_assistant, message=message)
return "Salary slip verification in progress. Please check the console for details."
def handle_tlsn_verification(self, proof_json):
"""Handle TLSN proof verification through TLSN assistant"""
if not proof_json:
return {"success": False, "error": "Empty proof provided"}
# Save proof
save_result = save_tlsn_proof(proof_json)
if not save_result["success"]:
return save_result
# Prepare message
message = f"Here's my TLSN proof for verification: [Proof begins]\n{proof_json[:500]}...[Proof trimmed]"
# Interact with TLSN assistant
self.user_proxy.initiate_chat(self.tlsn_assistant, message=message)
# Verify proof directly
result = verify_tlsn_proof(proof_json)
return result
def create_integrated_ui():
"""Create Gradio UI with Autogen integration"""
integration = AutogenIntegration()
with gr.Blocks(title="Lucid Loan Machine - Integrated") as app:
gr.Markdown("# 💰 Lucid Loan Machine (LLM) - Integrated with Autogen")
gr.Markdown("## A secure loan application system powered by AI agents 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():
raw_email_input = gr.Textbox(label="Raw Email Content", lines=15)
email_submit_button = gr.Button("Verify Email")
with gr.Column():
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():
tlsn_proof_input = gr.Textbox(label="TLSN Proof JSON", lines=15)
tlsn_submit_button = gr.Button("Verify TLSN Proof")
with gr.Column():
tlsn_verification_output = gr.JSON(label="TLSN Verification Result")
# Set up event handlers
submit_button.click(
integration.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(
integration.handle_email_verification,
inputs=[raw_email_input],
outputs=email_verification_output
)
pdf_submit_button.click(
integration.handle_salary_verification,
inputs=[pdf_url_input],
outputs=salary_verification_output
)
tlsn_submit_button.click(
integration.handle_tlsn_verification,
inputs=[tlsn_proof_input],
outputs=tlsn_verification_output
)
return app
if __name__ == "__main__":
app = create_integrated_ui()
app.launch()