Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,103 +1,73 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from faster_whisper import WhisperModel
|
| 3 |
from transformers import pipeline
|
| 4 |
-
from textblob import TextBlob
|
| 5 |
import numpy as np
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
import time
|
| 9 |
-
|
| 10 |
-
# Initialize shared state
|
| 11 |
-
audio_queue = queue.Queue()
|
| 12 |
-
transcription_results = []
|
| 13 |
-
ai_detection_results = []
|
| 14 |
-
|
| 15 |
-
# Global stop event for threads
|
| 16 |
-
stop_event = threading.Event()
|
| 17 |
|
| 18 |
def initialize_model():
|
| 19 |
-
"""Initialize Whisper model and AI
|
| 20 |
-
|
| 21 |
st.session_state.model = WhisperModel("small", device="cpu", compute_type="int8")
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def advanced_ai_detection(text, ai_detector):
|
| 27 |
-
"""Perform AI detection on
|
| 28 |
-
if len(text.split()) < 5:
|
| 29 |
-
return "Insufficient Data"
|
| 30 |
-
|
| 31 |
result = ai_detector(text)[0]
|
| 32 |
-
return result
|
| 33 |
-
|
| 34 |
-
def analyze_text_linguistics(text):
|
| 35 |
-
"""Perform linguistic analysis on the text."""
|
| 36 |
-
blob = TextBlob(text)
|
| 37 |
return {
|
| 38 |
-
"
|
| 39 |
-
"
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
-
def transcribe_audio(audio_chunk, model):
|
| 43 |
-
"""Transcribe audio using the Whisper model."""
|
| 44 |
-
segments, _ = model.transcribe(audio_chunk, language="en")
|
| 45 |
-
return [segment.text for segment in segments]
|
| 46 |
-
|
| 47 |
def run_app():
|
| 48 |
-
"""Main Streamlit app
|
| 49 |
st.title("AI Speech Detector")
|
| 50 |
-
st.subheader("
|
| 51 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
start_button = st.sidebar.button("Start Recording")
|
| 56 |
-
stop_button = st.sidebar.button("Stop Recording")
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
initialize_model()
|
| 61 |
-
st.text("Models loaded successfully!")
|
| 62 |
-
|
| 63 |
-
# Display transcript
|
| 64 |
-
st.text_area("Real-Time Transcript", height=200, key="transcript")
|
| 65 |
-
|
| 66 |
-
# Display AI detection results
|
| 67 |
-
st.text_area("AI Detection Results", height=200, key="ai_detection")
|
| 68 |
-
|
| 69 |
-
if start_button:
|
| 70 |
-
st.session_state.is_recording = True
|
| 71 |
-
threading.Thread(target=process_audio_stream).start()
|
| 72 |
-
|
| 73 |
-
if stop_button:
|
| 74 |
-
st.session_state.is_recording = False
|
| 75 |
-
stop_event.set()
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
model = st.session_state.model
|
| 80 |
-
ai_detector = st.session_state.ai_detector
|
| 81 |
-
|
| 82 |
-
while not stop_event.is_set():
|
| 83 |
try:
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
|
|
|
| 87 |
|
| 88 |
-
#
|
|
|
|
| 89 |
for text in transcription:
|
| 90 |
-
st.session_state.
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
st.
|
| 95 |
-
|
| 96 |
except Exception as e:
|
| 97 |
-
st.error(f"Error
|
| 98 |
-
break
|
| 99 |
-
|
| 100 |
-
stop_event.clear()
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
run_app()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from faster_whisper import WhisperModel
|
| 3 |
from transformers import pipeline
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
from textblob import TextBlob
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def initialize_model():
|
| 9 |
+
"""Initialize the Whisper model and AI detection pipeline."""
|
| 10 |
+
if "model" not in st.session_state:
|
| 11 |
st.session_state.model = WhisperModel("small", device="cpu", compute_type="int8")
|
| 12 |
+
if "ai_detector" not in st.session_state:
|
| 13 |
+
st.session_state.ai_detector = pipeline("text-classification", model="roberta-base-openai-detector")
|
| 14 |
+
|
| 15 |
+
def process_uploaded_audio(uploaded_file, model):
|
| 16 |
+
"""Process uploaded audio file for transcription."""
|
| 17 |
+
# Convert uploaded file to a WAV file
|
| 18 |
+
audio = AudioSegment.from_file(uploaded_file)
|
| 19 |
+
audio = audio.set_frame_rate(16000).set_channels(1)
|
| 20 |
+
samples = np.array(audio.get_array_of_samples(), dtype=np.float32) / 32768.0
|
| 21 |
+
|
| 22 |
+
# Perform transcription
|
| 23 |
+
segments, _ = model.transcribe(samples, language="en", vad_filter=True)
|
| 24 |
+
return [segment.text for segment in segments]
|
| 25 |
|
| 26 |
def advanced_ai_detection(text, ai_detector):
|
| 27 |
+
"""Perform AI detection on transcribed text."""
|
| 28 |
+
if len(text.split()) < 5:
|
| 29 |
+
return {"classification": "Insufficient Data", "probability": 0.0, "confidence": "Low"}
|
|
|
|
| 30 |
result = ai_detector(text)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return {
|
| 32 |
+
"classification": result["label"],
|
| 33 |
+
"probability": result["score"],
|
| 34 |
+
"confidence": "High" if result["score"] > 0.7 else "Medium" if result["score"] > 0.5 else "Low"
|
| 35 |
}
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def run_app():
|
| 38 |
+
"""Main Streamlit app."""
|
| 39 |
st.title("AI Speech Detector")
|
| 40 |
+
st.subheader("Upload an audio file for transcription and AI analysis.")
|
| 41 |
+
st.markdown("""
|
| 42 |
+
This app uses the Whisper model for speech-to-text transcription and AI detection to classify the text.
|
| 43 |
+
Supported audio formats: **.wav**, **.mp3**.
|
| 44 |
+
""")
|
| 45 |
|
| 46 |
+
# Initialize models
|
| 47 |
+
initialize_model()
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# File uploader
|
| 50 |
+
uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
if uploaded_file:
|
| 53 |
+
st.info("Processing audio... Please wait.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
+
# Transcription
|
| 56 |
+
transcription = process_uploaded_audio(uploaded_file, st.session_state.model)
|
| 57 |
+
full_transcript = "\n".join(transcription)
|
| 58 |
+
st.text_area("Transcription", value=full_transcript, height=300)
|
| 59 |
|
| 60 |
+
# AI Detection
|
| 61 |
+
st.subheader("AI Detection Results")
|
| 62 |
for text in transcription:
|
| 63 |
+
detection_result = advanced_ai_detection(text, st.session_state.ai_detector)
|
| 64 |
+
st.write(f"**Text:** {text}")
|
| 65 |
+
st.write(f"- **Classification:** {detection_result['classification']}")
|
| 66 |
+
st.write(f"- **Probability:** {detection_result['probability']:.2f}")
|
| 67 |
+
st.write(f"- **Confidence:** {detection_result['confidence']}")
|
| 68 |
+
st.markdown("---")
|
| 69 |
except Exception as e:
|
| 70 |
+
st.error(f"Error processing audio: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
run_app()
|