File size: 7,157 Bytes
fa77042 3f108f8 5874100 fa77042 3f108f8 fa77042 3f108f8 fa77042 dd8fec6 209b38f dd8fec6 209b38f dd8fec6 3f108f8 fa77042 209b38f fa77042 3f108f8 209b38f feabc0d 209b38f 46276fb 209b38f dd8fec6 209b38f feabc0d 209b38f feabc0d 209b38f feabc0d 209b38f feabc0d 209b38f feabc0d 209b38f feabc0d 209b38f fa77042 3f108f8 209b38f dd8fec6 fa77042 3f108f8 dd8fec6 3f108f8 dd8fec6 3f108f8 dd8fec6 fa77042 3f108f8 fa77042 3f108f8 dd8fec6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import streamlit as st
import requests
import io
import base64
BASE_URL = "https://hammad712-spam.hf.space"
st.title("AI Spam Blocker API Tester")
st.markdown("## Save Contact")
with st.form("save_contact_form"):
contact_phone = st.text_input("Enter Contact Phone (e.g., +1-555-901-2345)")
contact_name = st.text_input("Enter Contact Name (optional)", value="John Doe")
contact_email = st.text_input("Enter Contact Email (optional)", value="john.doe@example.com")
save_submitted = st.form_submit_button("Save Contact")
if save_submitted:
if not contact_phone:
st.error("Please enter a contact phone number.")
else:
payload = [{
"phone": contact_phone,
"name": contact_name if contact_name else None,
"email": contact_email if contact_email else None
}]
with st.spinner("Saving contact..."):
try:
response = requests.post(f"{BASE_URL}/contacts", json=payload)
if response.ok:
st.success("Contact saved successfully!")
else:
st.error(f"Error: {response.status_code} {response.text}")
except Exception as e:
st.error(f"Exception: {str(e)}")
st.markdown("---")
st.markdown("## Call Test")
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")
st.markdown("For spam calls, upload the fake audio message below.")
audio_file = st.file_uploader("Upload Fake Audio File", type=["m4a", "wav", "mp3"], key="spam_audio")
if st.button("Test Call"):
if not call_number:
st.error("Please enter a caller number.")
else:
# First, check if the contact is saved by calling GET /contacts/{phone}
with st.spinner("Checking contact..."):
contact_resp = requests.get(f"{BASE_URL}/contacts/{call_number}")
if contact_resp.ok:
# Contact exists β show ringing message immediately.
st.info(f"Call from {call_number} β Ringing")
else:
# No saved contact β treat as spam call.
st.markdown(f"**Spam call from {call_number} β Bot replying**")
# Ensure an audio file is uploaded.
if audio_file is None:
st.error("Please upload a fake audio file for the spam test.")
st.stop()
file_bytes = audio_file.read()
# ----- Step 1: Call the STT endpoint -----
with st.spinner("Processing STT..."):
files = {"audio": (audio_file.name, io.BytesIO(file_bytes), audio_file.type)}
stt_resp = requests.post(f"{BASE_URL}/stt", files=files)
if stt_resp.ok:
stt_data = stt_resp.json()
transcription = stt_data.get("transcription", "No transcription")
else:
st.error(f"STT Error: {stt_resp.status_code} {stt_resp.text}")
st.stop()
st.markdown(f"**STT:** Transcribed β{transcription}β")
# ----- Step 2: Call the LLM endpoint using the STT transcription -----
with st.spinner("Processing LLM..."):
llm_payload = {"message": transcription}
llm_resp = requests.post(f"{BASE_URL}/llm", json=llm_payload)
if llm_resp.ok:
llm_data = llm_resp.json()
llm_reply = llm_data.get("reply", "No reply")
else:
st.error(f"LLM Error: {llm_resp.status_code} {llm_resp.text}")
st.stop()
st.markdown(f"**LLM:** Bot says β{llm_reply}β")
# ----- Step 3: Call the TTS endpoint with the LLM reply -----
with st.spinner("Processing TTS..."):
tts_payload = {"text": llm_reply}
tts_resp = requests.post(f"{BASE_URL}/tts", json=tts_payload)
if tts_resp.ok:
tts_data = tts_resp.json()
tts_audio_b64 = tts_data.get("audio", "")
else:
st.error(f"TTS Error: {tts_resp.status_code} {tts_resp.text}")
st.stop()
st.markdown("**TTS:** Bot audio generated from LLM response")
if tts_audio_b64:
audio_bytes = base64.b64decode(tts_audio_b64)
st.audio(audio_bytes, format="audio/wav")
else:
st.info("No TTS audio returned.")
st.markdown("---")
st.markdown("## Text Test")
text_test_type = st.radio("Select Text Test Type", ("Contact Test", "Spam Test", "Unknown Test"))
text_number = st.text_input("Enter Sender Number for Text Test (e.g., +1-555-901-2345 for contact):", key="text_number")
default_text = "Hi" if text_test_type == "Contact Test" else "Free trip!" if text_test_type == "Spam Test" else "Fix your car!"
text_message = st.text_input("Enter Text Message", value=default_text, key="text_message")
if st.button("Test Text"):
if not text_number or not text_message:
st.error("Please enter both a number and a message.")
else:
payload = {"phone": text_number, "message": text_message}
with st.spinner("Processing text message..."):
try:
response = requests.post(f"{BASE_URL}/incoming-message", json=payload)
if response.ok:
data = response.json()
if "detail" in data:
st.success(f"Response: {data['detail']}")
else:
st.success("Conversation Result:\n" + data.get("conversation_result", ""))
else:
st.error(f"Error: {response.status_code} {response.text}")
except Exception as e:
st.error(f"Exception: {str(e)}")
st.markdown("---")
st.markdown("## Setup Test")
if st.button("Start Setup"):
with st.spinner("Setting up call forwarding..."):
try:
response = requests.post(f"{BASE_URL}/setup-call-forwarding")
if response.ok:
st.success(f"Response: {response.json()['message']}")
else:
st.error(f"Error: {response.status_code} {response.text}")
except Exception as e:
st.error(f"Exception: {str(e)}")
st.markdown("---")
st.markdown("## Messages History")
msg_number = st.text_input("Enter Caller Number to Retrieve Conversation History", key="msg_number")
if st.button("Get Messages"):
if not msg_number:
st.error("Please enter a caller number.")
else:
with st.spinner("Retrieving messages..."):
try:
response = requests.get(f"{BASE_URL}/messages/{msg_number}")
if response.ok:
data = response.json()
st.write(data)
else:
st.error(f"Error: {response.status_code} {response.text}")
except Exception as e:
st.error(f"Exception: {str(e)}")
|