kanha-upadhyay commited on
Commit
ee310ff
·
1 Parent(s): d104988

Rename project from "test-interface" to "kora" and update dependencies: replace Twilio with Plivo.

Browse files
Files changed (4) hide show
  1. app.py +41 -34
  2. poetry.lock +0 -0
  3. pyproject.toml +2 -2
  4. requirements.txt +1 -1
app.py CHANGED
@@ -6,9 +6,9 @@ import time
6
  import urllib.parse
7
  from http.server import BaseHTTPRequestHandler, HTTPServer
8
 
 
9
  import streamlit as st
10
  from dotenv import load_dotenv
11
- from twilio.rest import Client
12
 
13
  # --- Setup logging ---
14
  logging.basicConfig(level=logging.INFO)
@@ -16,15 +16,14 @@ logging.basicConfig(level=logging.INFO)
16
  # --- Load environment variables ---
17
  load_dotenv()
18
 
19
- # --- Setup Twilio ---
20
  try:
21
- TWILIO_SID = os.getenv("TWILIO_SID")
22
- TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
23
- TWILIO_PHONE_NUMBER = os.getenv("TWILIO_PHONE_NUMBER")
24
  KORA_PHONE_NUMBER = os.getenv("KORA_PHONE_NUMBER")
25
- client = Client(TWILIO_SID, TWILIO_AUTH_TOKEN)
26
  except Exception as e:
27
- st.error(f"Error setting up Twilio: {e}")
28
 
29
  # --- Thread lock for SQLite access ---
30
  db_lock = threading.Lock()
@@ -59,13 +58,13 @@ class SMSWebhookHandler(BaseHTTPRequestHandler):
59
 
60
  logging.info(f"Webhook received: {data}")
61
  # Handle incoming SMS
62
- body = data.get("Body", [""])[0]
63
 
64
- if body:
65
  with db_lock:
66
  c.execute(
67
  "INSERT INTO messages (sender, text) VALUES (?, ?)",
68
- ("assistant", body),
69
  )
70
  conn.commit()
71
 
@@ -86,7 +85,7 @@ class SMSWebhookHandler(BaseHTTPRequestHandler):
86
  def run_webhook_server():
87
  try:
88
  server = HTTPServer(("", 8502), SMSWebhookHandler)
89
- logging.info("Webhook server started on port 8080")
90
  server.serve_forever()
91
  except Exception as e:
92
  logging.error(f"Error starting webhook server: {e}")
@@ -104,14 +103,21 @@ if not hasattr(st, "webhook_server_started"):
104
  # --- Streamlit UI ---
105
  st.title("📲 Chat with KORA")
106
 
 
 
 
 
107
  # Display chat history
108
  try:
109
  with db_lock:
110
  messages = c.execute(
111
- "SELECT sender, text FROM messages ORDER BY timestamp ASC"
 
112
  ).fetchall()
113
  for sender, text in messages:
114
- st.chat_message("user" if sender == "user" else "assistant").write(text)
 
 
115
  except sqlite3.Error as e:
116
  st.error(f"Error fetching messages: {e}")
117
 
@@ -121,35 +127,36 @@ if prompt := st.chat_input("Type your message..."):
121
  # Save to DB
122
  with db_lock:
123
  c.execute(
124
- "INSERT INTO messages (sender, text) VALUES (?, ?)", ("user", prompt)
 
125
  )
126
  conn.commit()
127
 
128
  # Show in UI
129
  st.chat_message("user").write(prompt)
130
 
131
- # Send via Twilio
132
  client.messages.create(
133
- body=prompt, from_=TWILIO_PHONE_NUMBER, to=KORA_PHONE_NUMBER
134
  )
135
-
136
- # Periodically check for new messages from the assistant
137
- def check_for_new_messages():
138
- while True:
139
- try:
140
- with db_lock:
141
- latest_message = c.execute(
142
- "SELECT sender, text FROM messages ORDER BY timestamp DESC LIMIT 1"
143
- ).fetchone()
144
- if latest_message and latest_message[0] == "assistant":
145
- st.chat_message("assistant").write(latest_message[1])
146
- break
147
- except sqlite3.Error as e:
148
- logging.error(f"Error checking for new messages: {e}")
149
- time.sleep(5)
150
-
151
- check_for_new_messages()
152
  except sqlite3.Error as e:
153
  st.error(f"Error saving message to database: {e}")
154
  except Exception as e:
155
- st.error(f"Error sending message via Twilio: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import urllib.parse
7
  from http.server import BaseHTTPRequestHandler, HTTPServer
8
 
9
+ import plivo
10
  import streamlit as st
11
  from dotenv import load_dotenv
 
12
 
13
  # --- Setup logging ---
14
  logging.basicConfig(level=logging.INFO)
 
16
  # --- Load environment variables ---
17
  load_dotenv()
18
 
19
+ # --- Setup plivo ---
20
  try:
21
+ PLIVO_AUTH_ID = os.getenv("PLIVO_AUTH_ID")
22
+ PLIVO_AUTH_TOKEN = os.getenv("PLIVO_AUTH_TOKEN")
 
23
  KORA_PHONE_NUMBER = os.getenv("KORA_PHONE_NUMBER")
24
+ client = plivo.RestClient(auth_id=PLIVO_AUTH_ID, auth_token=PLIVO_AUTH_TOKEN)
25
  except Exception as e:
26
+ st.error(f"Error setting up plivo: {e}")
27
 
28
  # --- Thread lock for SQLite access ---
29
  db_lock = threading.Lock()
 
58
 
59
  logging.info(f"Webhook received: {data}")
60
  # Handle incoming SMS
61
+ message_text = data.get("Text", [""])[0]
62
 
63
+ if message_text:
64
  with db_lock:
65
  c.execute(
66
  "INSERT INTO messages (sender, text) VALUES (?, ?)",
67
+ (KORA_PHONE_NUMBER, message_text),
68
  )
69
  conn.commit()
70
 
 
85
  def run_webhook_server():
86
  try:
87
  server = HTTPServer(("", 8502), SMSWebhookHandler)
88
+ logging.info("Webhook server started on port 8502")
89
  server.serve_forever()
90
  except Exception as e:
91
  logging.error(f"Error starting webhook server: {e}")
 
103
  # --- Streamlit UI ---
104
  st.title("📲 Chat with KORA")
105
 
106
+ PLIVO_PHONE_NUMBER = st.text_input(
107
+ "Enter your phone number to chat with KORA", value="+13602304837", max_chars=15
108
+ )
109
+
110
  # Display chat history
111
  try:
112
  with db_lock:
113
  messages = c.execute(
114
+ "SELECT sender, text FROM messages where sender IN (?, ?) ORDER BY timestamp ASC",
115
+ (PLIVO_PHONE_NUMBER, KORA_PHONE_NUMBER),
116
  ).fetchall()
117
  for sender, text in messages:
118
+ st.chat_message("user" if sender == PLIVO_PHONE_NUMBER else "assistant").write(
119
+ text
120
+ )
121
  except sqlite3.Error as e:
122
  st.error(f"Error fetching messages: {e}")
123
 
 
127
  # Save to DB
128
  with db_lock:
129
  c.execute(
130
+ "INSERT INTO messages (sender, text) VALUES (?, ?)",
131
+ (PLIVO_PHONE_NUMBER, prompt),
132
  )
133
  conn.commit()
134
 
135
  # Show in UI
136
  st.chat_message("user").write(prompt)
137
 
138
+ # Send via plivo
139
  client.messages.create(
140
+ text=prompt, src=PLIVO_PHONE_NUMBER, dst=KORA_PHONE_NUMBER
141
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  except sqlite3.Error as e:
143
  st.error(f"Error saving message to database: {e}")
144
  except Exception as e:
145
+ st.error(f"Error sending message via plivo: {e}")
146
+
147
+
148
+ def check_for_new_messages():
149
+ while True:
150
+ try:
151
+ with db_lock:
152
+ latest_message = c.execute(
153
+ "SELECT sender, text FROM messages ORDER BY timestamp DESC LIMIT 1"
154
+ ).fetchone()
155
+ if latest_message and latest_message[0] == KORA_PHONE_NUMBER:
156
+ st.chat_message("assistant").write(latest_message[1])
157
+ except sqlite3.Error as e:
158
+ logging.error(f"Error checking for new messages: {e}")
159
+ time.sleep(5)
160
+
161
+
162
+ check_for_new_messages()
poetry.lock CHANGED
The diff for this file is too large to render. See raw diff
 
pyproject.toml CHANGED
@@ -1,5 +1,5 @@
1
  [tool.poetry]
2
- name = "test-interface"
3
  version = "0.1.0"
4
  description = ""
5
  authors = ["Kanha Upadhyay <kanha.upadhyay@sifars.com>"]
@@ -8,8 +8,8 @@ readme = "README.md"
8
  [tool.poetry.dependencies]
9
  python = "3.12.*"
10
  streamlit = "^1.44.1"
11
- twilio = "^9.5.2"
12
  python-dotenv = "^1.1.0"
 
13
 
14
 
15
  [build-system]
 
1
  [tool.poetry]
2
+ name = "kora"
3
  version = "0.1.0"
4
  description = ""
5
  authors = ["Kanha Upadhyay <kanha.upadhyay@sifars.com>"]
 
8
  [tool.poetry.dependencies]
9
  python = "3.12.*"
10
  streamlit = "^1.44.1"
 
11
  python-dotenv = "^1.1.0"
12
+ plivo = "^4.58.6"
13
 
14
 
15
  [build-system]
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
  streamlit==1.44.1
2
- twilio==9.5.2
3
  python-dotenv==1.1.0
 
1
  streamlit==1.44.1
2
+ plivo==4.58.6
3
  python-dotenv==1.1.0