Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 queue
|
7 |
+
import threading
|
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 detectors."""
|
20 |
+
st.session_state.model = WhisperModel(model_size="small", device="cpu", compute_type="int8")
|
21 |
+
st.session_state.ai_detector = pipeline('text-classification', model='roberta-base-openai-detector')
|
22 |
+
|
23 |
+
def advanced_ai_detection(text, ai_detector):
|
24 |
+
"""Perform AI detection on the text."""
|
25 |
+
if len(text.split()) < 5: # Skip short texts
|
26 |
+
return "Insufficient Data"
|
27 |
+
|
28 |
+
result = ai_detector(text)[0]
|
29 |
+
return result
|
30 |
+
|
31 |
+
def analyze_text_linguistics(text):
|
32 |
+
"""Perform linguistic analysis on the text."""
|
33 |
+
blob = TextBlob(text)
|
34 |
+
return {
|
35 |
+
"sentiment": blob.sentiment.polarity,
|
36 |
+
"subjectivity": blob.sentiment.subjectivity
|
37 |
+
}
|
38 |
+
|
39 |
+
def transcribe_audio(audio_chunk, model):
|
40 |
+
"""Transcribe audio using the Whisper model."""
|
41 |
+
segments, _ = model.transcribe(audio_chunk, language="en")
|
42 |
+
return [segment.text for segment in segments]
|
43 |
+
|
44 |
+
def run_app():
|
45 |
+
"""Main Streamlit app function."""
|
46 |
+
st.title("AI Speech Detector")
|
47 |
+
st.subheader("Real-Time Speech Transcription and AI Detection")
|
48 |
+
st.text("This app transcribes audio input and detects if the text is AI-generated.")
|
49 |
+
|
50 |
+
# Sidebar for controls
|
51 |
+
st.sidebar.title("Controls")
|
52 |
+
start_button = st.sidebar.button("Start Recording")
|
53 |
+
stop_button = st.sidebar.button("Stop Recording")
|
54 |
+
|
55 |
+
if "model" not in st.session_state:
|
56 |
+
st.text("Loading AI models...")
|
57 |
+
initialize_model()
|
58 |
+
st.text("Models loaded successfully!")
|
59 |
+
|
60 |
+
# Display transcript
|
61 |
+
st.text_area("Real-Time Transcript", height=200, key="transcript")
|
62 |
+
|
63 |
+
# Display AI detection results
|
64 |
+
st.text_area("AI Detection Results", height=200, key="ai_detection")
|
65 |
+
|
66 |
+
if start_button:
|
67 |
+
st.session_state.is_recording = True
|
68 |
+
threading.Thread(target=process_audio_stream).start()
|
69 |
+
|
70 |
+
if stop_button:
|
71 |
+
st.session_state.is_recording = False
|
72 |
+
stop_event.set()
|
73 |
+
|
74 |
+
def process_audio_stream():
|
75 |
+
"""Simulated audio capture and processing."""
|
76 |
+
model = st.session_state.model
|
77 |
+
ai_detector = st.session_state.ai_detector
|
78 |
+
|
79 |
+
while not stop_event.is_set():
|
80 |
+
try:
|
81 |
+
# Simulate real-time audio input
|
82 |
+
fake_audio_chunk = np.random.rand(16000 * 3).astype(np.float32) # 3 seconds of fake audio
|
83 |
+
transcription = transcribe_audio(fake_audio_chunk, model)
|
84 |
+
|
85 |
+
# Update real-time transcript
|
86 |
+
for text in transcription:
|
87 |
+
st.session_state.transcript += text + "\n"
|
88 |
+
|
89 |
+
# Perform AI detection
|
90 |
+
ai_result = advanced_ai_detection(text, ai_detector)
|
91 |
+
st.session_state.ai_detection += f"Text: {text}\nResult: {ai_result}\n\n"
|
92 |
+
|
93 |
+
except Exception as e:
|
94 |
+
st.error(f"Error during transcription: {str(e)}")
|
95 |
+
break
|
96 |
+
|
97 |
+
stop_event.clear()
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
run_app()
|