Th3Nic3Guy commited on
Commit
dc4c54a
·
1 Parent(s): e4f82d4

Update for Sign Up Forms

Browse files
Files changed (3) hide show
  1. .gitignore +10 -0
  2. app.py +113 -20
  3. requirements.txt +2 -0
.gitignore CHANGED
@@ -1 +1,11 @@
1
  .venv/
 
 
 
 
 
 
 
 
 
 
 
1
  .venv/
2
+ __pycache__/
3
+ *.pyc
4
+ *.pyo
5
+ *.pyd
6
+ .env
7
+ *.ipynb
8
+ .ipynb_checkpoints/
9
+ *.log
10
+ *.log.*
11
+ *.log-*
app.py CHANGED
@@ -1,27 +1,38 @@
1
  """gradio app for Chat Interface for DataStax Langflow calls"""
2
 
3
- import json
 
4
  from typing import Optional, Sequence, Tuple
5
  from uuid import uuid4
 
6
  import requests
7
  import gradio as gr
8
- import os
 
 
 
9
 
10
  BASE_API_URL = "https://api.langflow.astra.datastax.com"
11
  LANGFLOW_ID = "e99b525a-84dd-4cf2-bac6-8e5ebc6a7d17"
12
  FLOW_ID = "bfbf7237-50dd-40dd-baba-cc680288d788"
13
- APPLICATION_TOKEN = os.getenv('DATASTAX_TOKEN')
 
 
 
 
 
 
14
 
15
  ENDPOINT = 'ai_traveller'
16
 
17
  TWEAKS = {
18
  "Memory-fCuCs": {
19
- "n_messages": 100,
20
- "order": "Ascending",
21
- "sender": "Machine and User",
22
- "sender_name": "",
23
- "template": "{sender_name}: {text}"
24
- },
25
  "ChatInput-PN5E4": {},
26
  "Prompt-vGvVG": {},
27
  "GoogleGenerativeAIModel-6n0Ft": {},
@@ -29,6 +40,52 @@ TWEAKS = {
29
  }
30
 
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def call_travel_ai(
33
  message: str,
34
  history: Sequence[Tuple[str, str]],
@@ -37,7 +94,7 @@ def call_travel_ai(
37
  output_type: str = "chat",
38
  input_type: str = "chat",
39
  tweaks: Optional[dict] = None,
40
- application_token: Optional[str] = APPLICATION_TOKEN
41
  ) -> dict:
42
  """
43
  Run a flow with a given message and optional tweaks.
@@ -47,6 +104,7 @@ def call_travel_ai(
47
  :param tweaks: Optional tweaks to customize the flow
48
  :return: The JSON response from the flow
49
  """
 
50
  api_url = f"{BASE_API_URL}/lf/{LANGFLOW_ID}/api/v1/run/{endpoint}"
51
 
52
  payload = {
@@ -78,27 +136,62 @@ def call_travel_ai(
78
 
79
  return resp_message
80
 
 
81
  def generate_session_id():
 
82
  return str(uuid4())
83
 
84
- def render_value(value):
85
- return value
86
 
87
  with gr.Blocks() as demo:
88
- session_id = gr.Textbox(
89
  value=generate_session_id,
90
  visible=False,
91
  interactive=False,
92
  label="Session ID",
93
  )
94
 
95
- chat_interface = gr.ChatInterface(
96
- call_travel_ai,
97
- type='messages',
98
- title=f"AI Travel Partner",
99
- additional_inputs=[session_id],
100
- autofocus=True,
101
- fill_height=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  )
103
 
104
  demo.launch(share=False, debug=True,)
 
1
  """gradio app for Chat Interface for DataStax Langflow calls"""
2
 
3
+ import re
4
+ from datetime import datetime
5
  from typing import Optional, Sequence, Tuple
6
  from uuid import uuid4
7
+ import os
8
  import requests
9
  import gradio as gr
10
+ from dotenv import load_dotenv
11
+
12
+ load_dotenv()
13
+
14
 
15
  BASE_API_URL = "https://api.langflow.astra.datastax.com"
16
  LANGFLOW_ID = "e99b525a-84dd-4cf2-bac6-8e5ebc6a7d17"
17
  FLOW_ID = "bfbf7237-50dd-40dd-baba-cc680288d788"
18
+ ASTRA_DB_CLIENT_ID = os.getenv('ASTRA_DB_CLIENT_ID')
19
+ ASTRA_DB_ID = os.getenv('ASTRA_DB_ID')
20
+ ASTRA_DB_KEYSPACE = os.getenv('ASTRA_DB_KEYSPACE')
21
+ ASTRA_DB_REGION = os.getenv('ASTRA_DB_REGION')
22
+ ASTRA_DB_SECRET = os.getenv('ASTRA_DB_SECRET')
23
+ ASTRA_DB_TOKEN = os.getenv('ASTRA_DB_TOKEN')
24
+ DATASTAX_TOKEN = os.getenv('DATASTAX_TOKEN')
25
 
26
  ENDPOINT = 'ai_traveller'
27
 
28
  TWEAKS = {
29
  "Memory-fCuCs": {
30
+ "n_messages": 100,
31
+ "order": "Ascending",
32
+ "sender": "Machine and User",
33
+ "sender_name": "",
34
+ "template": "{sender_name}: {text}"
35
+ },
36
  "ChatInput-PN5E4": {},
37
  "Prompt-vGvVG": {},
38
  "GoogleGenerativeAIModel-6n0Ft": {},
 
40
  }
41
 
42
 
43
+ def email_validator(_email: str) -> bool:
44
+ """RegEx based Validate the email address."""
45
+ return bool(re.match(r"[^@]+@[^@]+\.[^@]+", _email))
46
+
47
+
48
+ def contact_validator(_contact: str) -> bool:
49
+ """RegEx based Validate the contact number."""
50
+ return bool(re.match(r'^\d{10}$', _contact))
51
+
52
+
53
+ def required_validator(_name: str, _email: str, _contact: str) -> bool:
54
+ """Validate the required fields."""
55
+ return bool(_name.strip() and _email.strip() and _contact.strip())
56
+
57
+
58
+ def submit_signup(_email: str, _name: str, _contact: str):
59
+ """Add signups to the database."""
60
+
61
+ if not email_validator(_email):
62
+ return 'Invalid Email'
63
+
64
+ if not contact_validator(_contact):
65
+ return 'Invalid Contact Number'
66
+
67
+ if not required_validator(_name, _email, _contact):
68
+ return 'All Fields Required for Sign Up'
69
+
70
+ response = requests.post(
71
+ f'https://{ASTRA_DB_ID}-{ASTRA_DB_REGION}.apps.astra.datastax.com/api/rest/v2/keyspaces/{ASTRA_DB_KEYSPACE}/signups',
72
+ headers={
73
+ 'Content-Type': 'application/json',
74
+ 'X-Cassandra-Token': ASTRA_DB_TOKEN
75
+ },
76
+ json={
77
+ 'email': _email,
78
+ 'name': _name,
79
+ 'contact_no': _contact,
80
+ 'created_at': datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
81
+ },
82
+ timeout=360
83
+ )
84
+ if response.status_code != 201:
85
+ return 'Error'
86
+ return 'Success'
87
+
88
+
89
  def call_travel_ai(
90
  message: str,
91
  history: Sequence[Tuple[str, str]],
 
94
  output_type: str = "chat",
95
  input_type: str = "chat",
96
  tweaks: Optional[dict] = None,
97
+ application_token: Optional[str] = DATASTAX_TOKEN
98
  ) -> dict:
99
  """
100
  Run a flow with a given message and optional tweaks.
 
104
  :param tweaks: Optional tweaks to customize the flow
105
  :return: The JSON response from the flow
106
  """
107
+ del history
108
  api_url = f"{BASE_API_URL}/lf/{LANGFLOW_ID}/api/v1/run/{endpoint}"
109
 
110
  payload = {
 
136
 
137
  return resp_message
138
 
139
+
140
  def generate_session_id():
141
+ """Generate a unique session ID."""
142
  return str(uuid4())
143
 
 
 
144
 
145
  with gr.Blocks() as demo:
146
+ random_session_id = gr.Textbox(
147
  value=generate_session_id,
148
  visible=False,
149
  interactive=False,
150
  label="Session ID",
151
  )
152
 
153
+ with gr.Accordion(
154
+ 'Sign Up',
155
+ ) as signup:
156
+
157
+ with gr.Column():
158
+ name = gr.Textbox(label="Name")
159
+ email = gr.Textbox(label="Email")
160
+ phone = gr.Textbox(label="Phone")
161
+ submit_button = gr.Button("Submit")
162
+
163
+ with gr.Accordion(
164
+ 'Chat With Agent',
165
+ visible=False,
166
+ ) as chat:
167
+ chat_interface = gr.ChatInterface(
168
+ call_travel_ai,
169
+ type='messages',
170
+ title="AI Travel Partner",
171
+ additional_inputs=[random_session_id],
172
+ autofocus=True,
173
+ fill_height=True,
174
+ )
175
+
176
+ def update_visibility(
177
+ _name: str,
178
+ _email: str,
179
+ _phone: str,
180
+
181
+ ):
182
+ """Update the visibility of the sign up and chat interfaces."""
183
+ _status = submit_signup(_email, _name, _phone)
184
+ gr.Info(_status, duration=5, title='Sign Up Status')
185
+
186
+ return (
187
+ gr.update(visible=_status != 'Success'),
188
+ gr.update(visible=_status == 'Success')
189
+ )
190
+
191
+ submit_button.click( # pylint: disable=no-member
192
+ update_visibility,
193
+ [name, email, phone],
194
+ outputs=[signup, chat],
195
  )
196
 
197
  demo.launch(share=False, debug=True,)
requirements.txt CHANGED
@@ -2,3 +2,5 @@ gradio
2
  pandas
3
  requests
4
  numpy
 
 
 
2
  pandas
3
  requests
4
  numpy
5
+ cassandra-driver
6
+ python-dotenv