Hammad712 commited on
Commit
209b38f
·
verified ·
1 Parent(s): 5874100

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -45
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import requests
3
  import io
4
  import base64
 
5
 
6
  # Set the base URL for your FastAPI server
7
  BASE_URL = "https://hammad712-spam.hf.space"
@@ -13,9 +14,9 @@ with st.form("save_contact_form"):
13
  contact_phone = st.text_input("Enter Contact Phone (e.g., +1-555-901-2345)")
14
  contact_name = st.text_input("Enter Contact Name (optional)", value="John Doe")
15
  contact_email = st.text_input("Enter Contact Email (optional)", value="john.doe@example.com")
16
- submitted = st.form_submit_button("Save Contact")
17
 
18
- if submitted:
19
  if not contact_phone:
20
  st.error("Please enter a contact phone number.")
21
  else:
@@ -36,62 +37,83 @@ if submitted:
36
 
37
  st.markdown("---")
38
  st.markdown("## Call Test")
39
- call_test_type = st.radio("Select Call Test Type", ("Contact Test", "Spam Test"))
40
- call_number = st.text_input("Enter Caller Number (e.g., +1-555-901-2345 or +1-555-654-3210):", key="call_number")
41
 
42
- if call_test_type == "Spam Test":
43
- st.markdown("For spam calls, upload the fake audio message below.")
44
- audio_file = st.file_uploader("Upload Fake Audio File", type=["m4a", "wav", "mp3"])
45
- else:
46
- st.markdown("For contact calls, the message is not used because a saved contact returns a ringing response.")
47
- dummy_message = "dummy"
48
- audio_file = None
49
 
50
  if st.button("Test Call"):
51
  if not call_number:
52
  st.error("Please enter a caller number.")
53
  else:
54
- if call_test_type == "Spam Test":
 
 
 
 
 
 
 
 
 
 
 
55
  if audio_file is None:
56
  st.error("Please upload a fake audio file for the spam test.")
57
- else:
58
- dummy_audio = audio_file.read()
59
- files = {"audio": (audio_file.name, io.BytesIO(dummy_audio), audio_file.type)}
60
- else:
61
- dummy_audio = io.BytesIO(dummy_message.encode("utf-8"))
62
- files = {"audio": ("audio.m4a", dummy_audio, "audio/m4a")}
63
- data = {"caller_number": call_number}
64
- with st.spinner("Processing call..."):
65
- try:
66
- response = requests.post(f"{BASE_URL}/process-call", data=data, files=files)
67
- if response.ok:
68
- st.success(f"Response: {response.json()['message']}")
69
- # After process-call is successful, get the audio reply
70
- with st.spinner("Retrieving audio response..."):
71
- audio_reply_resp = requests.get(f"{BASE_URL}/audio-reply/{call_number}")
72
- if audio_reply_resp.ok:
73
- audio_data = audio_reply_resp.json()
74
- st.markdown("### Audio Reply Details")
75
- st.write("**STT Response:**", audio_data.get("stt_response", ""))
76
- st.write("**LLM Reply:**", audio_data.get("llm_reply", ""))
77
- # Decode the base64 audio and display an audio player
78
- audio_b64 = audio_data.get("audio_reply", "")
79
- if audio_b64:
80
- audio_bytes = base64.b64decode(audio_b64)
81
- st.audio(audio_bytes, format="audio/wav")
82
- else:
83
- st.info("No audio reply available.")
84
- else:
85
- st.error(f"Error retrieving audio reply: {audio_reply_resp.status_code} {audio_reply_resp.text}")
86
  else:
87
- st.error(f"Error: {response.status_code} {response.text}")
88
- except Exception as e:
89
- st.error(f"Exception: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  st.markdown("---")
92
  st.markdown("## Text Test")
93
  text_test_type = st.radio("Select Text Test Type", ("Contact Test", "Spam Test", "Unknown Test"))
94
- text_number = st.text_input("Enter Sender Number for Text Test (e.g., +1-555-901-2345 for contact or other for unsaved):", key="text_number")
95
  default_text = "Hi" if text_test_type == "Contact Test" else "Free trip!" if text_test_type == "Spam Test" else "Fix your car!"
96
  text_message = st.text_input("Enter Text Message", value=default_text, key="text_message")
97
 
 
2
  import requests
3
  import io
4
  import base64
5
+ import time
6
 
7
  # Set the base URL for your FastAPI server
8
  BASE_URL = "https://hammad712-spam.hf.space"
 
14
  contact_phone = st.text_input("Enter Contact Phone (e.g., +1-555-901-2345)")
15
  contact_name = st.text_input("Enter Contact Name (optional)", value="John Doe")
16
  contact_email = st.text_input("Enter Contact Email (optional)", value="john.doe@example.com")
17
+ save_submitted = st.form_submit_button("Save Contact")
18
 
19
+ if save_submitted:
20
  if not contact_phone:
21
  st.error("Please enter a contact phone number.")
22
  else:
 
37
 
38
  st.markdown("---")
39
  st.markdown("## Call Test")
 
 
40
 
41
+ call_number = st.text_input("Enter Caller Number (e.g., +1-555-654-3210 for spam or +1-555-901-2345 for contact):", key="call_number")
42
+
43
+ # For this task, we only simulate spam calls if the caller is not found in contacts.
44
+ st.markdown("For spam calls, upload the fake audio message below.")
45
+ audio_file = st.file_uploader("Upload Fake Audio File", type=["m4a", "wav", "mp3"], key="spam_audio")
 
 
46
 
47
  if st.button("Test Call"):
48
  if not call_number:
49
  st.error("Please enter a caller number.")
50
  else:
51
+ # First, check if the contact is saved by calling GET /contacts/{phone}
52
+ with st.spinner("Checking contact..."):
53
+ contact_resp = requests.get(f"{BASE_URL}/contacts/{call_number}")
54
+ if contact_resp.ok:
55
+ # Contact exists – show ringing message immediately.
56
+ st.info(f"Call from {call_number} – Ringing")
57
+ else:
58
+ # No saved contact → treat as spam call.
59
+ placeholder = st.empty()
60
+ placeholder.markdown(f"**Spam call from {call_number} – Bot replying**")
61
+
62
+ # Ensure an audio file is uploaded.
63
  if audio_file is None:
64
  st.error("Please upload a fake audio file for the spam test.")
65
+ st.stop()
66
+ file_bytes = audio_file.read()
67
+
68
+ # ----- Step 1: Call the STT endpoint -----
69
+ with st.spinner("Processing STT..."):
70
+ files = {"audio": (audio_file.name, io.BytesIO(file_bytes), audio_file.type)}
71
+ stt_resp = requests.post(f"{BASE_URL}/stt", files=files)
72
+ if stt_resp.ok:
73
+ stt_data = stt_resp.json()
74
+ transcription = stt_data.get("transcription", "No transcription")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  else:
76
+ st.error(f"STT Error: {stt_resp.status_code} {stt_resp.text}")
77
+ st.stop()
78
+ time.sleep(5) # Wait for 5 seconds
79
+ placeholder.markdown(f"**STT:** Transcribed ‘{transcription}’")
80
+
81
+ # ----- Step 2: Call the LLM endpoint using the transcription as input -----
82
+ with st.spinner("Processing LLM..."):
83
+ llm_payload = {"message": transcription}
84
+ llm_resp = requests.post(f"{BASE_URL}/llm", json=llm_payload)
85
+ if llm_resp.ok:
86
+ llm_data = llm_resp.json()
87
+ llm_reply = llm_data.get("reply", "No reply")
88
+ else:
89
+ st.error(f"LLM Error: {llm_resp.status_code} {llm_resp.text}")
90
+ st.stop()
91
+ time.sleep(10) # Wait additional 10 seconds (total 15 sec from start)
92
+ placeholder.markdown(f"**LLM:** Bot says ‘{llm_reply}’")
93
+
94
+ # ----- Step 3: Call the TTS endpoint with a fixed text ("Tell me more!") -----
95
+ with st.spinner("Processing TTS..."):
96
+ tts_payload = {"text": "Tell me more!"}
97
+ tts_resp = requests.post(f"{BASE_URL}/tts", json=tts_payload)
98
+ if tts_resp.ok:
99
+ tts_data = tts_resp.json()
100
+ tts_audio_b64 = tts_data.get("audio", "")
101
+ else:
102
+ st.error(f"TTS Error: {tts_resp.status_code} {tts_resp.text}")
103
+ st.stop()
104
+ time.sleep(10) # Wait additional 10 seconds (total 25 sec from start)
105
+ placeholder.markdown(f"**TTS:** Bot says ‘Tell me more!’")
106
+
107
+ if tts_audio_b64:
108
+ audio_bytes = base64.b64decode(tts_audio_b64)
109
+ st.audio(audio_bytes, format="audio/wav")
110
+ else:
111
+ st.info("No TTS audio returned.")
112
 
113
  st.markdown("---")
114
  st.markdown("## Text Test")
115
  text_test_type = st.radio("Select Text Test Type", ("Contact Test", "Spam Test", "Unknown Test"))
116
+ text_number = st.text_input("Enter Sender Number for Text Test (e.g., +1-555-901-2345 for contact):", key="text_number")
117
  default_text = "Hi" if text_test_type == "Contact Test" else "Free trip!" if text_test_type == "Spam Test" else "Fix your car!"
118
  text_message = st.text_input("Enter Text Message", value=default_text, key="text_message")
119