Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

added chat support

Murtaza Nasir71e6585c7b85
diff --git a/app.py b/app.py
index 9d1c064f..4f4146ce 100644
--- a/app.py
+++ b/app.py
@@ -238,6 +238,73 @@ JSON Response:""" # The prompt guides the model towards the desired output
db.session.commit()
+# --- Chat with Transcription ---
[email protected]('/chat', methods=['POST'])
+def chat_with_transcription():
+ try:
+ data = request.json
+ if not data:
+ return jsonify({'error': 'No data provided'}), 400
+
+ recording_id = data.get('recording_id')
+ user_message = data.get('message')
+
+ if not recording_id:
+ return jsonify({'error': 'No recording ID provided'}), 400
+ if not user_message:
+ return jsonify({'error': 'No message provided'}), 400
+
+ # Get the recording
+ recording = db.session.get(Recording, recording_id)
+ if not recording:
+ return jsonify({'error': 'Recording not found'}), 404
+
+ # Check if OpenRouter client is available
+ if client is None:
+ return jsonify({'error': 'Chat service is not available (OpenRouter client not configured)'}), 503
+
+ # Prepare the system prompt with the transcription
+ system_prompt = f"""You are a professional meeting analyst working with Murtaza Nasir, Assistant Professor at Wichita State University. Analyze the following meeting information and respond to the specific request.
+
+Following are the meeting participants and their roles:
+{recording.participants or "No specific participants information provided."}
+
+Following is the meeting transcript:
+<<start transcript>>
+{recording.transcription or "No transcript available."}
+<<end transcript>>
+
+Additional context and notes about the meeting:
+{recording.notes or "none"}
+"""
+
+ # Call the LLM
+ try:
+ completion = client.chat.completions.create(
+ model=openrouter_model_name,
+ messages=[
+ {"role": "system", "content": system_prompt},
+ {"role": "user", "content": user_message}
+ ],
+ temperature=0.7,
+ max_tokens=1000
+ )
+
+ response_content = completion.choices[0].message.content
+
+ return jsonify({
+ 'response': response_content,
+ 'success': True
+ })
+
+ except Exception as chat_error:
+ app.logger.error(f"Error calling OpenRouter API for chat: {str(chat_error)}")
+ return jsonify({'error': f'Chat service error: {str(chat_error)}'}), 500
+
+ except Exception as e:
+ app.logger.error(f"Error in chat endpoint: {str(e)}")
+ return jsonify({'error': str(e)}), 500
+
# --- Flask Routes ---
@app.route('/')
@@ -404,4 +471,4 @@ if __name__ == '__main__':
# Consider using waitress or gunicorn for production
# waitress-serve --host 0.0.0.0 --port 8899 app:app
# For development:
- app.run(host='0.0.0.0', port=8899, debug=True) # Set debug=False if thread issues arise
\ No newline at end of file
+ app.run(host='0.0.0.0', port=8899, debug=True) # Set debug=False if thread issues arise
diff --git a/project_files.md b/project_files.md
index 20e6cc0b..177855e6 100644
--- a/project_files.md
+++ b/project_files.md
@@ -131,6 +131,7 @@ from sqlalchemy import select
import threading
from dotenv import load_dotenv # Import load_dotenv
import httpx
+import re
# Load environment variables from .env file
load_dotenv()
@@ -294,9 +295,22 @@ JSON Response:""" # The prompt guides the model towards the desired output
response_content = completion.choices[0].message.content
app.logger.debug(f"Raw OpenRouter response for {recording_id}: {response_content}")
-
- # Parse the JSON response
+
try:
+ response_content = completion.choices[0].message.content
+ app.logger.debug(f"Raw OpenRouter response for {recording_id}: {response_content}")
+
+ # Use regex to extract JSON content from potential markdown code blocks
+ # This looks for content between markdown code blocks or just takes the whole content
+ json_match = re.search(r'```(?:json)?(.*?)```|(.+)', response_content, re.DOTALL)
+
+ if json_match:
+ # Use the first group that matched (either between ``` or the whole content)
+ sanitized_response = json_match.group(1) if json_match.group(1) else json_match.group(2)
+ sanitized_response = sanitized_response.strip()
+ else:
+ sanitized_response = response_content.strip()
+
summary_data = json.loads(response_content)
generated_title = summary_data.get("title")
generated_summary = summary_data.get("summary")
diff --git a/templates/index.html b/templates/index.html
index 839aad42..93c7ebec 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -47,6 +47,77 @@
.status-completed { color: #065f46; background-color: #d1fae5; } /* text-green-800 bg-green-100 */
.status-failed { color: #991b1b; background-color: #fee2e2; } /* text-red-800 bg-red-100 */
.status-pending { color: #57534e; background-color: #f5f5f4; } /* text-stone-700 bg-stone-100 */
+
+ /* Fixed height transcription box */
+ .transcription-box {
+ height: 400px;
+ overflow-y: auto;
+ position: relative;
+ }
+
+ /* Copy button styles */
+ .copy-btn {
+ position: sticky;
+ top: 10px;
+ right: 10px;
+ float: right;
+ background-color: rgba(255, 255, 255, 0.8);
+ border: 1px solid #e5e7eb;
+ border-radius: 0.375rem;
+ padding: 0.25rem 0.5rem;
+ font-size: 0.75rem;
+ cursor: pointer;
+ z-index: 10;
+ transition: all 0.2s ease-in-out;
+ margin-bottom: 10px;
+ }
+
+ .copy-btn:hover {
+ background-color: #f3f4f6;
+ }
+
+ /* Chat section styles */
+ .chat-container {
+ border: 1px solid #e5e7eb;
+ border-radius: 0.375rem;
+ display: flex;
+ flex-direction: column;
+ height: 400px;
+ }
+
+ .chat-messages {
+ flex-grow: 1;
+ overflow-y: auto;
+ padding: 1rem;
+ }
+
+ .chat-input-container {
+ border-top: 1px solid #e5e7eb;
+ padding: 0.75rem;
+ display: flex;
+ }
+
+ .message {
+ margin-bottom: 1rem;
+ max-width: 80%;
+ }
+
+ .user-message {
+ background-color: #dbeafe;
+ border-radius: 1rem 1rem 0.25rem 1rem;
+ padding: 0.75rem;
+ margin-left: auto;
+ }
+
+ .ai-message {
+ background-color: #f3f4f6;
+ border-radius: 1rem 1rem 1rem 0.25rem;
+ padding: 0.75rem;
+ }
+
+ .copyable {
+ position: relative;
+ }
</style>
</head>
@@ -174,23 +245,43 @@
</div>
<div class="grid md:grid-cols-2 gap-6 flex-grow overflow-hidden">
- <div class="flex flex-col overflow-hidden">
- <h4 class="font-semibold text-gray-700 mb-2 flex-shrink-0">Transcription</h4>
- <div class="flex-grow overflow-y-auto p-4 bg-gray-50 rounded border border-gray-200 text-sm custom-scrollbar">
- <div v-if="selectedRecording.status === 'COMPLETED' || selectedRecording.status === 'SUMMARIZING'">
- <pre class="whitespace-pre-wrap font-sans">${ selectedRecording.transcription || 'No transcription available.' }</pre>
- </div>
- <div v-else-if="selectedRecording.status === 'FAILED'" class="text-red-700">
- <p class="font-medium mb-2">Processing Failed:</p>
- <pre class="whitespace-pre-wrap font-sans">${ selectedRecording.transcription || 'An unknown error occurred.' }</pre>
+ <div class="flex flex-col overflow-hidden space-y-4">
+ <div class="flex-grow">
+ <h4 class="font-semibold text-gray-700 mb-2 flex-shrink-0">Transcription</h4>
+ <div class="transcription-box p-4 bg-gray-50 rounded border border-gray-200 text-sm custom-scrollbar relative">
+ <button @click="copyTranscription" class="copy-btn" title="Copy to clipboard">
+ <i class="fas fa-copy mr-1"></i> Copy
+ </button>
+ <div v-if="selectedRecording.status === 'COMPLETED' || selectedRecording.status === 'SUMMARIZING'" class="copyable">
+ <pre class="whitespace-pre-wrap font-sans">${ selectedRecording.transcription || 'No transcription available.' }</pre>
+ </div>
+ <div v-else-if="selectedRecording.status === 'FAILED'" class="text-red-700">
+ <p class="font-medium mb-2">Processing Failed:</p>
+ <pre class="whitespace-pre-wrap font-sans">${ selectedRecording.transcription || 'An unknown error occurred.' }</pre>
+ </div>
+ <div v-else class="flex items-center justify-center text-gray-500 h-full">
+ <i class="fas fa-spinner fa-spin mr-2"></i> Transcription in progress...
+ </div>
</div>
- <div v-else class="flex items-center justify-center text-gray-500 h-full">
- <i class="fas fa-spinner fa-spin mr-2"></i> Transcription in progress...
+ </div>
+
+ <div>
+ <h4 class="font-semibold text-gray-700 mb-2 flex-shrink-0">Summary</h4>
+ <div class="flex-grow overflow-hidden">
+ <div v-if="selectedRecording.status === 'COMPLETED'" class="summary-box custom-scrollbar">
+ ${ selectedRecording.summary || 'No summary generated.' }
+ </div>
+ <div v-else-if="selectedRecording.status === 'FAILED'" class="summary-box text-red-700 custom-scrollbar">
+ ${ selectedRecording.summary || 'Summary generation failed or was skipped.' }
+ </div>
+ <div v-else class="h-24 flex items-center justify-center p-4 bg-gray-50 rounded border border-gray-200 text-gray-500">
+ <i class="fas fa-spinner fa-spin mr-2"></i> Summary pending...
+ </div>
</div>
</div>
</div>
- <div class="flex flex-col space-y-4 overflow-hidden">
+ <div class="flex flex-col space-y-4 overflow-hidden">
<div>
<h4 class="font-semibold text-gray-700 mb-2">Audio Player</h4>
<audio controls class="w-full" :key="selectedRecording.id" :src="'/audio/' + selectedRecording.id">
@@ -198,17 +289,46 @@
</audio>
</div>
- <div class="flex flex-col overflow-hidden">
- <h4 class="font-semibold text-gray-700 mb-2 flex-shrink-0">Summary</h4>
- <div class="flex-grow overflow-hidden">
- <div v-if="selectedRecording.status === 'COMPLETED'" class="summary-box custom-scrollbar">
- ${ selectedRecording.summary || 'No summary generated.' }
- </div>
- <div v-else-if="selectedRecording.status === 'FAILED'" class="summary-box text-red-700 custom-scrollbar">
- ${ selectedRecording.summary || 'Summary generation failed or was skipped.' }
+ <div class="flex flex-col overflow-hidden flex-grow">
+ <h4 class="font-semibold text-gray-700 mb-2 flex-shrink-0">Chat with Transcript</h4>
+ <div class="chat-container">
+ <div class="chat-messages custom-scrollbar" ref="chatMessagesRef">
+ <div v-if="chatMessages.length === 0" class="flex items-center justify-center h-full text-gray-500">
+ <div class="text-center">
+ <i class="fas fa-comments text-2xl mb-2"></i>
+ <p>Ask questions about the transcript</p>
+ </div>
+ </div>
+ <div v-for="(message, index) in chatMessages" :key="index" class="message" :class="message.role === 'user' ? 'user-message' : 'ai-message'">
+ <div class="flex justify-between items-start">
+ <div class="flex-grow">${ message.content }</div>
+ <button v-if="message.role === 'assistant'" @click="copyMessage(message.content)" class="ml-2 text-gray-400 hover:text-gray-600" title="Copy response">
+ <i class="fas fa-copy text-xs"></i>
+ </button>
+ </div>
+ </div>
+ <div v-if="isChatLoading" class="message ai-message">
+ <div class="flex items-center">
+ <i class="fas fa-spinner fa-spin mr-2"></i> Thinking...
+ </div>
+ </div>
</div>
- <div v-else class="h-24 flex items-center justify-center p-4 bg-gray-50 rounded border border-gray-200 text-gray-500">
- <i class="fas fa-spinner fa-spin mr-2"></i> Summary pending...
+ <div class="chat-input-container">
+ <input
+ v-model="chatInput"
+ @keyup.enter="sendChatMessage"
+ type="text"
+ placeholder="Ask about the transcript..."
+ class="flex-grow px-3 py-2 border-0 focus:ring-0 focus:outline-none"
+ :disabled="isChatLoading || !selectedRecording || selectedRecording.status !== 'COMPLETED'"
+ >
+ <button
+ @click="sendChatMessage"
+ class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 disabled:bg-gray-300 disabled:cursor-not-allowed"
+ :disabled="!chatInput.trim() || isChatLoading || !selectedRecording || selectedRecording.status !== 'COMPLETED'"
+ >
+ <i class="fas fa-paper-plane"></i>
+ </button>
</div>
</div>
</div>
@@ -217,11 +337,10 @@
<h4 class="font-semibold text-gray-700 mb-1">Participants</h4>
<p class="text-sm bg-gray-50 p-3 rounded border border-gray-200 min-h-[40px]">${ selectedRecording.participants || 'None specified' }</p>
</div>
- <div class="flex flex-col overflow-hidden flex-grow">
- <h4 class="font-semibold text-gray-700 mb-1 flex-shrink-0">Notes</h4>
- <div class="flex-grow overflow-hidden">
- <pre class="text-sm bg-gray-50 p-3 rounded border border-gray-200 whitespace-pre-wrap h-40 overflow-y-auto custom-scrollbar font-sans">${ selectedRecording.notes || 'No notes' }</pre>
- </div>
+
+ <div class="flex flex-col overflow-hidden">
+ <h4 class="font-semibold text-gray-700 mb-1">Notes</h4>
+ <pre class="text-sm bg-gray-50 p-3 rounded border border-gray-200 whitespace-pre-wrap h-40 overflow-y-auto custom-scrollbar font-sans">${ selectedRecording.notes || 'No notes' }</pre>
</div>
</div>
</div>
@@ -934,6 +1053,101 @@
}, { deep: true });
+ // --- Chat functionality ---
+ const showChat = ref(false);
+ const chatMessages = ref([]);
+ const chatInput = ref('');
+ const isChatLoading = ref(false);
+
+ // Create a ref for the chat messages container
+ const chatMessagesRef = ref(null);
+
+ const sendChatMessage = async () => {
+ if (!chatInput.value.trim() || isChatLoading.value || !selectedRecording.value || selectedRecording.value.status !== 'COMPLETED') {
+ return;
+ }
+
+ const message = chatInput.value.trim();
+
+ // Ensure chatMessages.value is an array before pushing
+ if (!Array.isArray(chatMessages.value)) {
+ console.warn('chatMessages.value was not an array! Resetting. Value was:', chatMessages.value);
+ chatMessages.value = []; // Reset if corrupted
+ }
+
+ chatMessages.value.push({ role: 'user', content: message });
+ chatInput.value = '';
+ isChatLoading.value = true;
+
+ // Scroll to bottom of chat
+ await nextTick();
+ // Use the correctly named ref
+ if (chatMessagesRef.value) {
+ chatMessagesRef.value.scrollTop = chatMessagesRef.value.scrollHeight;
+ }
+
+ try {
+ const response = await fetch('/chat', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ recording_id: selectedRecording.value.id,
+ message: message
+ })
+ });
+
+ const data = await response.json();
+ if (!response.ok) throw new Error(data.error || 'Failed to get chat response');
+
+ chatMessages.value.push({ role: 'assistant', content: data.response });
+ } catch (error) {
+ console.error('Chat Error:', error);
+ setGlobalError(`Chat error: ${error.message}`);
+ chatMessages.value.push({ role: 'assistant', content: `Error: ${error.message}` });
+ } finally {
+ isChatLoading.value = false;
+ // Scroll to bottom of chat
+ await nextTick();
+ // Use the correctly named ref
+ if (chatMessagesRef.value) {
+ chatMessagesRef.value.scrollTop = chatMessagesRef.value.scrollHeight;
+ }
+ }
+ };
+
+ const copyMessage = (text) => {
+ navigator.clipboard.writeText(text)
+ .then(() => {
+ setGlobalError('Response copied to clipboard!', 2000);
+ })
+ .catch(err => {
+ console.error('Copy failed:', err);
+ setGlobalError('Failed to copy: ' + err.message);
+ });
+ };
+
+ const copyTranscription = () => {
+ if (!selectedRecording.value || !selectedRecording.value.transcription) {
+ setGlobalError('No transcription available to copy.');
+ return;
+ }
+
+ navigator.clipboard.writeText(selectedRecording.value.transcription)
+ .then(() => {
+ setGlobalError('Transcription copied to clipboard!', 2000);
+ })
+ .catch(err => {
+ console.error('Copy failed:', err);
+ setGlobalError('Failed to copy: ' + err.message);
+ });
+ };
+
+ // Clear chat messages when recording changes
+ watch(selectedRecording, () => {
+ chatMessages.value = [];
+ showChat.value = false;
+ });
+
return {
// State
currentView, dragover, recordings, selectedRecording, // currentRecording removed
@@ -942,6 +1156,8 @@
// Multi-upload State
uploadQueue, currentlyProcessingFile, processingProgress, processingMessage,
isProcessingActive, progressPopupMinimized,
+ // Chat State
+ showChat, chatMessages, chatInput, isChatLoading, chatMessagesRef, // <-- Added chatMessagesRef
// Computed
groupedRecordings, totalInQueue, completedInQueue, queuedFiles, finishedFilesInQueue,
// Methods
@@ -949,6 +1165,8 @@
selectRecording, editRecording, cancelEdit, saveEdit, confirmDelete,
cancelDelete, deleteRecording, switchToUploadView, switchToGalleryView,
formatFileSize, setGlobalError, handleDragLeave, formatStatus, getStatusClass,
+ // Chat Methods
+ sendChatMessage, copyMessage, copyTranscription,
}
},
delimiters: ['${', '}'] // Keep Vue delimiters distinct from Flask's Jinja
@@ -956,4 +1174,4 @@
</script>
</body>
-</html>
\ No newline at end of file
+</html>