update repo code, run instructions, update to packages

This commit is contained in:
hackertron
2025-03-05 19:28:52 +08:00
parent f9da7a5f83
commit 911e660dd5
7 changed files with 632 additions and 20 deletions

3
.gitignore vendored
View File

@@ -163,3 +163,6 @@ cython_debug/
# tmp folders
tmp/

View File

@@ -1,11 +1,10 @@
[
{
"model": "gpt-3.5-turbo",
"api_key": "api-key-here"
},
{
"model": "gpt-4",
"api_key": "api-key-here"
}
]
{
"model": "gpt-3.5-turbo",
"api_key": "api-key-here"
},
{
"model": "gpt-4",
"api_key": "api-key-here"
}
]

View File

@@ -6,7 +6,12 @@ Lucid Loand Machine(LLM) : LLM and zk powered loan app
2. tlsnNotary
3. zkemail
## quick demo/try
1. install requirements.txt
2. run python main.py
## architecture
need to complete
### notes

268
gradio_app.py Normal file
View File

@@ -0,0 +1,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()

267
integrated_app.py Normal file
View File

@@ -0,0 +1,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()

20
main.py
View File

@@ -1,8 +1,8 @@
from autogen import AssistantAgent, UserProxyAgent, config_list_from_json
from typing import Annotated
import requests
from system_prompts import front_desk_assistant_prompt, email_assistant_prompt, verify_tlsn_proof_prompt
from system_prompts import front_desk_assistant_prompt, email_assistant_prompt, verify_tlsn_proof_prompt, salary_slip_assistant_prompt
from verify_tlsn_proof import verify_tlsn_proof, save_tlsn_proof
llm_config = {
@@ -36,11 +36,11 @@ verify_tlsn_proof_assistant = AssistantAgent(
system_message=verify_tlsn_proof_prompt
)
# salary_slip_assistant = AssistantAgent(
# name="salary_slip_assistant",
# llm_config=llm_config,
# system_message=salary_slip_assistant_prompt
# )
salary_slip_assistant = AssistantAgent(
name="salary_slip_assistant",
llm_config=llm_config,
system_message=salary_slip_assistant_prompt
)
user_proxy = UserProxyAgent(
name="user_proxy",
@@ -62,15 +62,15 @@ user_proxy = UserProxyAgent(
user_proxy.register_for_llm(name="verify_email_with_prove_api", description="verify email's dkim using prove api verify_email_with_prove_api")(verify_email_with_prove_api)
user_proxy.register_for_execution(name="verify_email_with_prove_api")(verify_email_with_prove_api)
#user_proxy.register_for_llm(name="process_pdf_from_url", description="process pdf from url using extract_pdf_skill")(process_pdf_from_url)
#user_proxy.register_for_execution(name="process_pdf_from_url")(process_pdf_from_url)
user_proxy.register_for_llm(name="verify_tlsn_proof", description="verify tlsn json proof")(verify_tlsn_proof)
user_proxy.register_for_execution(name="verify_tlsn_proof")(verify_tlsn_proof)
def main():
# Register the verify_email_with_prove_api function for the email_assistant
email_assistant.register_function(
function_map={
"verify_email_with_prove_api": verify_email_with_prove_api,
#"process_pdf_from_url": process_pdf_from_url
"verify_tlsn_proof": verify_tlsn_proof
}
)
chat_results = user_proxy.initiate_chats([

View File

@@ -0,0 +1,70 @@
import json
import requests
from typing import Dict, Any, Annotated
def verify_tlsn_proof(proof_json: Annotated[str, "JSON content of the TLSN proof"]) -> Dict[str, Any]:
"""
Verify a TLSN proof by uploading it to explorer.tlsn.org
Args:
proof_json (str): The JSON content of the TLSN proof
Returns:
Dict[str, Any]: A dictionary containing the verification result
"""
try:
# Parse the JSON to ensure it's valid
proof_data = json.loads(proof_json)
# Upload to explorer.tlsn.org
# This is a simplified implementation - you may need to adjust based on actual API
response = requests.post(
"https://explorer.tlsn.org/api/verify",
json=proof_data,
headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
result = response.json()
# Extract relevant information like name and country
return {
"success": True,
"verification_result": result,
"name": result.get("name", "Not found"),
"country": result.get("country", "Not found"),
"account_number": result.get("account_number", "Not found")
}
else:
return {
"success": False,
"error": f"Verification failed: {response.status_code}",
"message": response.text
}
except json.JSONDecodeError:
return {"success": False, "error": "Invalid JSON format"}
except Exception as e:
return {"success": False, "error": str(e)}
def save_tlsn_proof(proof_json: str) -> Dict[str, Any]:
"""
Save TLSN proof to a file
Args:
proof_json (str): The JSON content of the TLSN proof
Returns:
Dict[str, Any]: Result of the save operation
"""
try:
# Parse JSON to validate format
json.loads(proof_json)
# Save to file
with open("tlsn_proof.json", "w") as f:
f.write(proof_json)
return {"success": True, "message": "TLSN proof saved successfully"}
except json.JSONDecodeError:
return {"success": False, "error": "Invalid JSON format"}
except Exception as e:
return {"success": False, "error": str(e)}