Vadhana commited on
Commit
43578d8
Β·
verified Β·
1 Parent(s): 631ce33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -78
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 Real-Time Speech-to-Text, Sentiment Analysis, Summarization & POS Tagging")
36
-
37
- # Store text history for undo/redo
38
- if "text_history" not in st.session_state:
39
- st.session_state.text_history = [""]
40
- st.session_state.history_index = 0
41
-
42
- def get_current_text():
43
- return st.session_state.text_history[st.session_state.history_index]
44
-
45
- def update_text(new_text):
46
- # Save history for undo/redo
47
- st.session_state.text_history = st.session_state.text_history[:st.session_state.history_index + 1]
48
- st.session_state.text_history.append(new_text)
49
- st.session_state.history_index += 1
50
-
51
- # Speech Recognition
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 "" # Clear text
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 (Happy, Sad, Positive, Negative, Neutral, Fact-based)
91
  def analyze_sentiment(text):
92
- result = sentiment_analyzer(text[:512])[0]
93
- if result["label"] == "POSITIVE":
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
- # Real-Time Voice Command
114
  if st.button("πŸŽ™οΈ Speak Command"):
115
  with st.spinner("Listening..."):
116
- command = recognize_speech()
 
117
  st.write(f"Command Recognized: {command}")
118
 
119
  # Process the command
120
- processed_text = process_command(command, get_current_text())
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(get_current_text())
130
  st.success(sentiment)
131
 
132
  if st.button("πŸ“ Summarize Text"):
133
- summary = summarize_text(get_current_text())
134
  st.success(summary)
135
 
136
  if st.button("πŸ” Show POS Tags"):
137
- pos_tags = pos_tagging(get_current_text())
138
  st.write("πŸ”Ž POS Tags:", pos_tags)
139
-
140
  if st.button("❌ Clear Text"):
141
- update_text("")
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