Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,7 @@ import streamlit as st
|
|
2 |
import whisper
|
3 |
import torch
|
4 |
import spacy
|
5 |
-
import speech_recognition as sr
|
6 |
from transformers import pipeline
|
7 |
-
import tempfile
|
8 |
|
9 |
# Load spaCy Model
|
10 |
try:
|
@@ -32,37 +30,23 @@ whisper_model = load_whisper()
|
|
32 |
|
33 |
# Streamlit UI
|
34 |
st.title("ποΈ Voice-Controlled AI Text Editor")
|
35 |
-
st.subheader("Supports
|
36 |
-
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
#
|
52 |
-
def recognize_speech():
|
53 |
-
recognizer = sr.Recognizer()
|
54 |
-
with sr.Microphone() as source:
|
55 |
-
st.write("π€ Listening... Speak now!")
|
56 |
-
recognizer.adjust_for_ambient_noise(source)
|
57 |
-
audio = recognizer.listen(source)
|
58 |
-
|
59 |
-
with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as temp_audio:
|
60 |
-
temp_audio.write(audio.get_wav_data())
|
61 |
-
temp_audio.flush()
|
62 |
-
result = whisper_model.transcribe(temp_audio.name)
|
63 |
-
return result["text"]
|
64 |
-
|
65 |
-
# Command Processing
|
66 |
def process_command(command, text):
|
67 |
command = command.lower()
|
68 |
if "summarize" in command:
|
@@ -70,13 +54,7 @@ def process_command(command, text):
|
|
70 |
elif "analyze sentiment" in command:
|
71 |
return analyze_sentiment(text)
|
72 |
elif "delete" in command:
|
73 |
-
return ""
|
74 |
-
elif "undo" in command and st.session_state.history_index > 0:
|
75 |
-
st.session_state.history_index -= 1
|
76 |
-
return get_current_text()
|
77 |
-
elif "redo" in command and st.session_state.history_index < len(st.session_state.text_history) - 1:
|
78 |
-
st.session_state.history_index += 1
|
79 |
-
return get_current_text()
|
80 |
else:
|
81 |
return text # Return original text if no command is matched
|
82 |
|
@@ -87,38 +65,25 @@ def summarize_text(text):
|
|
87 |
summary = summarizer(text[:1024], max_length=100, min_length=30, do_sample=False)
|
88 |
return summary[0]['summary_text']
|
89 |
|
90 |
-
# Sentiment Analysis Function
|
91 |
def analyze_sentiment(text):
|
92 |
-
result = sentiment_analyzer(text[:512])
|
93 |
-
|
94 |
-
if result["score"] > 0.85:
|
95 |
-
mood = "π Happy"
|
96 |
-
else:
|
97 |
-
mood = "π Positive"
|
98 |
-
elif result["label"] == "NEGATIVE":
|
99 |
-
if result["score"] > 0.85:
|
100 |
-
mood = "π’ Sad"
|
101 |
-
else:
|
102 |
-
mood = "π Negative"
|
103 |
-
else:
|
104 |
-
mood = "π§ Neutral / Fact-based"
|
105 |
-
|
106 |
-
return f"Sentiment: {mood} (Confidence: {result['score']:.2f})"
|
107 |
|
108 |
# POS Tagging Function
|
109 |
def pos_tagging(text):
|
110 |
doc = nlp(text)
|
111 |
return [f"{token.text} -> {token.pos_}" for token in doc]
|
112 |
|
113 |
-
#
|
114 |
if st.button("ποΈ Speak Command"):
|
115 |
with st.spinner("Listening..."):
|
116 |
-
|
|
|
117 |
st.write(f"Command Recognized: {command}")
|
118 |
|
119 |
# Process the command
|
120 |
-
processed_text = process_command(command,
|
121 |
-
update_text(processed_text)
|
122 |
st.text_area("Processed Text", processed_text, height=200)
|
123 |
|
124 |
# Sidebar Options
|
@@ -126,34 +91,21 @@ with st.sidebar:
|
|
126 |
st.header("β‘ Actions")
|
127 |
|
128 |
if st.button("π Analyze Sentiment"):
|
129 |
-
sentiment = analyze_sentiment(
|
130 |
st.success(sentiment)
|
131 |
|
132 |
if st.button("π Summarize Text"):
|
133 |
-
summary = summarize_text(
|
134 |
st.success(summary)
|
135 |
|
136 |
if st.button("π Show POS Tags"):
|
137 |
-
pos_tags = pos_tagging(
|
138 |
st.write("π POS Tags:", pos_tags)
|
139 |
-
|
140 |
if st.button("β Clear Text"):
|
141 |
-
|
142 |
st.success("Text cleared.")
|
143 |
|
144 |
-
if st.button("β©οΈ Undo"):
|
145 |
-
if st.session_state.history_index > 0:
|
146 |
-
st.session_state.history_index -= 1
|
147 |
-
st.success("Undo successful!")
|
148 |
-
|
149 |
-
if st.button("βͺοΈ Redo"):
|
150 |
-
if st.session_state.history_index < len(st.session_state.text_history) - 1:
|
151 |
-
st.session_state.history_index += 1
|
152 |
-
st.success("Redo successful!")
|
153 |
-
|
154 |
-
# Display text area
|
155 |
-
st.text_area("π Live Text Editor", get_current_text(), height=200)
|
156 |
-
|
157 |
|
158 |
|
159 |
|
|
|
2 |
import whisper
|
3 |
import torch
|
4 |
import spacy
|
|
|
5 |
from transformers import pipeline
|
|
|
6 |
|
7 |
# Load spaCy Model
|
8 |
try:
|
|
|
30 |
|
31 |
# Streamlit UI
|
32 |
st.title("ποΈ Voice-Controlled AI Text Editor")
|
33 |
+
st.subheader("Supports Speech-to-Text, Sentiment Analysis, Summarization & POS Tagging")
|
34 |
+
|
35 |
+
# File Upload for Whisper
|
36 |
+
uploaded_audio = st.file_uploader("π΅ Upload an audio file", type=["wav", "mp3", "m4a"])
|
37 |
+
|
38 |
+
if uploaded_audio:
|
39 |
+
st.audio(uploaded_audio, format="audio/wav")
|
40 |
+
with open("temp_audio.wav", "wb") as f:
|
41 |
+
f.write(uploaded_audio.read())
|
42 |
+
|
43 |
+
with st.spinner("Transcribing..."):
|
44 |
+
result = whisper_model.transcribe("temp_audio.wav")
|
45 |
+
text = result["text"]
|
46 |
+
st.success("Transcription Complete!")
|
47 |
+
st.text_area("Transcribed Text", text, height=200)
|
48 |
+
|
49 |
+
# Function to process voice commands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
def process_command(command, text):
|
51 |
command = command.lower()
|
52 |
if "summarize" in command:
|
|
|
54 |
elif "analyze sentiment" in command:
|
55 |
return analyze_sentiment(text)
|
56 |
elif "delete" in command:
|
57 |
+
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
else:
|
59 |
return text # Return original text if no command is matched
|
60 |
|
|
|
65 |
summary = summarizer(text[:1024], max_length=100, min_length=30, do_sample=False)
|
66 |
return summary[0]['summary_text']
|
67 |
|
68 |
+
# Sentiment Analysis Function
|
69 |
def analyze_sentiment(text):
|
70 |
+
result = sentiment_analyzer(text[:512])
|
71 |
+
return f"Sentiment: {result[0]['label']} (Confidence: {result[0]['score']:.2f})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
# POS Tagging Function
|
74 |
def pos_tagging(text):
|
75 |
doc = nlp(text)
|
76 |
return [f"{token.text} -> {token.pos_}" for token in doc]
|
77 |
|
78 |
+
# Voice Command for Summarization or Sentiment Analysis
|
79 |
if st.button("ποΈ Speak Command"):
|
80 |
with st.spinner("Listening..."):
|
81 |
+
result = whisper_model.transcribe("temp_audio.wav")
|
82 |
+
command = result["text"]
|
83 |
st.write(f"Command Recognized: {command}")
|
84 |
|
85 |
# Process the command
|
86 |
+
processed_text = process_command(command, text)
|
|
|
87 |
st.text_area("Processed Text", processed_text, height=200)
|
88 |
|
89 |
# Sidebar Options
|
|
|
91 |
st.header("β‘ Actions")
|
92 |
|
93 |
if st.button("π Analyze Sentiment"):
|
94 |
+
sentiment = analyze_sentiment(text)
|
95 |
st.success(sentiment)
|
96 |
|
97 |
if st.button("π Summarize Text"):
|
98 |
+
summary = summarize_text(text)
|
99 |
st.success(summary)
|
100 |
|
101 |
if st.button("π Show POS Tags"):
|
102 |
+
pos_tags = pos_tagging(text)
|
103 |
st.write("π POS Tags:", pos_tags)
|
104 |
+
|
105 |
if st.button("β Clear Text"):
|
106 |
+
text = ""
|
107 |
st.success("Text cleared.")
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
|
111 |
|