Dineshpopuri commited on
Commit
21fef23
·
verified ·
1 Parent(s): 5c9decb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import pandas as pd
4
+
5
+ def create_contact_from_can(cands_from_trigger):
6
+ """
7
+ Process a list of candidate dictionaries to create contact records.
8
+
9
+ Args:
10
+ cands_from_trigger (list): List of dictionaries with candidate data.
11
+
12
+ Returns:
13
+ list: List of contact dictionaries.
14
+ """
15
+ con_list = []
16
+
17
+ for candidate in cands_from_trigger:
18
+ contact = {
19
+ "FirstName": candidate.get("First_Name__c", ""),
20
+ "LastName": candidate.get("Last_Name__c", ""),
21
+ "Email": candidate.get("Email__c", "")
22
+ }
23
+ con_list.append(contact)
24
+
25
+ return con_list
26
+
27
+ def process_json_input(json_input):
28
+ """
29
+ Process JSON input from Gradio text area and return contact records.
30
+
31
+ Args:
32
+ json_input (str): JSON string containing candidate data.
33
+
34
+ Returns:
35
+ str: JSON string of contact records or error message.
36
+ """
37
+ try:
38
+ # Parse JSON input (expecting a list of dictionaries)
39
+ candidates = json.loads(json_input)
40
+ if not isinstance(candidates, list):
41
+ return "Error: Input must be a JSON list of candidate objects."
42
+
43
+ # Process candidates
44
+ contacts = create_contact_from_can(candidates)
45
+
46
+ # Return formatted JSON
47
+ return json.dumps(contacts, indent=4)
48
+ except json.JSONDecodeError:
49
+ return "Error: Invalid JSON format. Please provide valid JSON, e.g., [{\"First_Name__c\": \"John\", \"Last_Name__c\": \"Doe\", \"Email__c\": \"john.doe@example.com\"}]"
50
+ except Exception as e:
51
+ return f"Error: {str(e)}"
52
+
53
+ def process_form_input(first_name, last_name, email):
54
+ """
55
+ Process form input for a single candidate and return contact record.
56
+
57
+ Args:
58
+ first_name (str): Candidate's first name.
59
+ last_name (str): Candidate's last name.
60
+ email (str): Candidate's email.
61
+
62
+ Returns:
63
+ str: JSON string of the contact record.
64
+ """
65
+ candidate = {
66
+ "First_Name__c": first_name,
67
+ "Last_Name__c": last_name,
68
+ "Email__c": email
69
+ }
70
+ contacts = create_contact_from_can([candidate])
71
+ return json.dumps(contacts[0], indent=4)
72
+
73
+ # Define Gradio interface
74
+ with gr.Blocks(title="Candidate to Contact Converter") as demo:
75
+ gr.Markdown("# Candidate to Contact Converter")
76
+ gr.Markdown("Convert candidate data to contact records. Use the JSON input for batch processing or the form for a single candidate.")
77
+
78
+ # JSON Input Section
79
+ gr.Markdown("## Batch Input (JSON)")
80
+ gr.Markdown("Enter candidate data in JSON format (list of objects). Example:")
81
+ gr.Markdown("""
82
+ ```json
83
+ [
84
+ {"First_Name__c": "John", "Last_Name__c": "Doe", "Email__c": "john.doe@example.com"},
85
+ {"First_Name__c": "Jane", "Last_Name__c": "Smith", "Email__c": "jane.smith@example.com"}
86
+ ]
87
+ ```
88
+ """)
89
+ json_input = gr.Textbox(label="Candidate JSON", lines=10)
90
+ json_output = gr.Textbox(label="Contact Output (JSON)", lines=10, interactive=False)
91
+ json_button = gr.Button("Convert JSON")
92
+ json_button.click(fn=process_json_input, inputs=json_input, outputs=json_output)
93
+
94
+ # Form Input Section
95
+ gr.Markdown("## Single Candidate Form")
96
+ first_name = gr.Textbox(label="First Name")
97
+ last_name = gr.Textbox(label="Last Name")
98
+ email = gr.Textbox(label="Email")
99
+ form_output = gr.Textbox(label="Contact Output (JSON)", lines=5, interactive=False)
100
+ form_button = gr.Button("Convert Form")
101
+ form_button.click(fn=process_form_input, inputs=[first_name, last_name, email], outputs=form_output)
102
+
103
+ # Download Output
104
+ gr.Markdown("## Download Results")
105
+ download_button = gr.File(label="Download Contacts as JSON")
106
+
107
+ def prepare_download(json_output):
108
+ if json_output and json_output.startswith("["):
109
+ with open("contacts.json", "w") as f:
110
+ f.write(json_output)
111
+ return "contacts.json"
112
+ return None
113
+
114
+ json_output.change(fn=prepare_download, inputs=json_output, outputs=download_button)
115
+
116
+ # Launch the app
117
+ demo.launch()