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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import streamlit as st
import os
import google.generativeai as genai
from google.ai.generativelanguage_v1beta.types import content
import json
from tempfile import NamedTemporaryFile
from datetime import datetime
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
import smtplib
from email.mime.text import MIMEText
from streamlit_mic_recorder import mic_recorder
import wave

# Install streamlit-mic-recorder if not already installed:
# pip install streamlit-mic-recorder

# Initialize session state for chat history if it doesn't exist
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

if 'diarization_output' not in st.session_state:
    st.session_state.diarization_output = None

if 'uploaded_file' not in st.session_state:
    st.session_state.uploaded_file = None

if 'language' not in st.session_state:
    st.session_state.language = "English"

if 'num_speakers' not in st.session_state:
    st.session_state.num_speakers = 2

if 'summary_output' not in st.session_state:
    st.session_state.summary_output = None

if 'key_decisions_output' not in st.session_state:
    st.session_state.key_decisions_output = None

if 'email_sent_message' not in st.session_state:
    st.session_state.email_sent_message = ""

if 'recorded_audio' not in st.session_state:
    st.session_state.recorded_audio = None


# Configuration for the page
st.set_page_config(
    page_title="AI Meeting Notes & Reporting",
    layout="wide"
)

# Function to generate PDF report
def generate_pdf_report(meeting_date, summary, key_decisions, transcription):
    buffer = io.BytesIO()
    p = canvas.Canvas(buffer, pagesize=letter)
    p.setFont("Helvetica-Bold", 16)
    p.drawString(inch, 10.5*inch, "Meeting Report")
    p.setFont("Helvetica", 12)
    p.drawString(inch, 10*inch, f"Date: {meeting_date.strftime('%Y-%m-%d')}")

    y_position = 9.5*inch

    p.setFont("Helvetica-Bold", 12)
    p.drawString(inch, y_position, "Summary:")
    y_position -= 0.3*inch
    p.setFont("Helvetica", 10)
    summary_lines = summary.split('\n')
    for line in summary_lines:
        p.drawString(inch, y_position, line)
        y_position -= 0.2*inch
        if y_position < 1*inch: # Simple page break to avoid content overflow - improve if needed
            p.showPage()
            y_position = 10.5*inch
            p.setFont("Helvetica", 10)


    if key_decisions:
        p.setFont("Helvetica-Bold", 12)
        p.drawString(inch, y_position, "Key Decisions:")
        y_position -= 0.3*inch
        p.setFont("Helvetica", 10)
        key_decisions_list = key_decisions.strip().split('\n')
        for decision in key_decisions_list:
            if decision.strip():
                p.drawString(inch, y_position, f"- {decision.strip()}")
                y_position -= 0.2*inch
                if y_position < 1*inch: # Simple page break
                    p.showPage()
                    y_position = 10.5*inch
                    p.setFont("Helvetica", 10)

    if transcription:
        p.setFont("Helvetica-Bold", 12)
        p.drawString(inch, y_position, "Transcription:")
        y_position -= 0.3*inch
        p.setFont("Helvetica", 8) # Smaller font for transcription
        transcription_lines = transcription.split('\n')
        for line in transcription_lines:
            p.drawString(inch, y_position, line)
            y_position -= 0.15*inch # Reduced line spacing for transcription
            if y_position < 1*inch: # Simple page break
                p.showPage()
                y_position = 10.5*inch
                p.setFont("Helvetica", 8)


    p.save()
    pdf_out = buffer.getvalue()
    buffer.close()
    return pdf_out

def send_email_report(email_address, meeting_date, summary, key_decisions, transcription):
    smtp_server = os.environ.get("SMTP_SERVER")
    smtp_port = os.environ.get("SMTP_PORT")
    smtp_username = os.environ.get("SMTP_USERNAME")
    smtp_password = os.environ.get("SMTP_PASSWORD")
    sender_email = smtp_username # For simplicity, assuming sender is the same as username

    if not all([smtp_server, smtp_port, smtp_username, smtp_password, sender_email]):
        return False, "SMTP configuration is missing. Please set environment variables: SMTP_SERVER, SMTP_PORT, SMTP_USERNAME, SMTP_PASSWORD."

    subject = f"Meeting Report - {meeting_date.strftime('%Y-%m-%d')}"
    body = f"Meeting Date: {meeting_date.strftime('%Y-%m-%d')}\n\nSummary:\n{summary}\n\nKey Decisions:\n{key_decisions}\n\nTranscription:\n{transcription}"

    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = sender_email
    msg['To'] = email_address

    try:
        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(smtp_username, smtp_password)
            server.sendmail(sender_email, email_address, msg.as_string())
        return True, "Email sent successfully!"
    except Exception as e:
        return False, f"Email sending failed: {e}"


# Main UI
st.title("AI Meeting Notes & Reporting")

# Meeting Date & Time
meeting_date_time = st.date_input("Meeting Date & Time", datetime.today())

# Number of speakers
num_speakers = st.number_input("Number of speakers", min_value=1, max_value=10, value=st.session_state.num_speakers)
st.session_state.num_speakers = num_speakers # Update session state

# Language selection
language = st.selectbox(
    "Language of report",
    ["English", "Turkish", "Spanish", "French", "German"],
    index=["English", "Turkish", "Spanish", "French", "German"].index(st.session_state.language) if st.session_state.language in ["English", "Turkish", "Spanish", "French", "German"] else 0
)
st.session_state.language = language # Update session state

# File upload
uploaded_file = st.file_uploader("Upload audio file", type=['mp3', 'wav'])

# Voice recording
audio_bytes = mic_recorder(start_prompt="Record", stop_prompt="Stop recording", key='recorder')

if audio_bytes:
    if isinstance(audio_bytes, dict) and "bytes" in audio_bytes: # Check if audio_bytes is a dict and has 'bytes' key
        st.audio(audio_bytes["bytes"], format="audio/wav")
        st.session_state.recorded_audio = audio_bytes["bytes"]
    else: # If not a dict or doesn't have 'bytes' key, assume it's raw bytes (fallback, might need adjustment)
        st.audio(audio_bytes, format="audio/wav")
        st.session_state.recorded_audio = audio_bytes


# Diarization, Summarization and Key Decisions logic - Automatically after upload or record
process_audio = False
audio_source_indicator = ""

if uploaded_file and uploaded_file != st.session_state.uploaded_file: # Check if a new file is uploaded
    st.session_state.uploaded_file = uploaded_file # Update session state
    st.session_state.recorded_audio = None # Reset recorded audio
    process_audio = True
    audio_source_indicator = f"Processing uploaded file: {uploaded_file.name}"
elif st.session_state.recorded_audio and st.session_state.recorded_audio != getattr(st.session_state.get('last_recorded_audio_hash'), 'value', None): # Check if new recording
    st.session_state.last_recorded_audio_hash = st.session_state.recorded_audio # Store hash to detect new recordings
    st.session_state.uploaded_file = None # Reset uploaded file
    process_audio = True
    audio_source_indicator = "Processing recorded audio"


if process_audio:
    st.session_state.diarization_output = None # Reset previous diarization output
    st.session_state.summary_output = None # Reset previous summary output
    st.session_state.key_decisions_output = None # Reset previous key decisions output
    st.session_state.chat_history = [] # Clear chat history for new file
    st.session_state.email_sent_message = "" # Clear email sent message

    with st.spinner(f"Processing audio and generating summary and key decisions... {audio_source_indicator}"):
        temp_path = None
        try:
            # Configure Gemini
            genai.configure(api_key=os.environ["GEMINI_API_KEY"])

            if st.session_state.uploaded_file: # Process uploaded file
                # Save uploaded file temporarily
                with NamedTemporaryFile(delete=False, suffix='.mp3') as tmp_file: # Assuming mp3 for wider compatibility, could adjust based on uploaded file type
                    tmp_file.write(st.session_state.uploaded_file.getvalue())
                    temp_path = tmp_file.name
                    mime_type = "audio/mpeg" # Assuming mp3, adjust if needed based on file type
                    gemini_file = genai.upload_file(temp_path, mime_type=mime_type)

            elif st.session_state.recorded_audio: # Process recorded audio
                # Save recorded audio temporarily (WAV from mic_recorder) and convert to MP3 if needed for Gemini
                with NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file_wav:
                    tmp_file_wav.write(st.session_state.recorded_audio)
                    temp_path = tmp_file_wav.name
                    gemini_file = genai.upload_file(temp_path, mime_type="audio/wav") # Assuming WAV is directly compatible


            # --- Diarization ---
            diarization_config = {
                "temperature": 0.5,
                "top_p": 0.95, #0.95
                "top_k": 40,
                "max_output_tokens": 8192,
                "response_mime_type": "application/json",
            }

            diarization_model = genai.GenerativeModel(
                model_name="gemini-2.0-flash-exp",
                generation_config=diarization_config,
                safety_settings={
                    'HATE': 'BLOCK_NONE',
                    'HARASSMENT': 'BLOCK_NONE',
                    'SEXUAL': 'BLOCK_NONE',
                    'DANGEROUS': 'BLOCK_NONE'
                }
            )

            chat_session_diarization = diarization_model.start_chat(
                history=[{"role": "user", "parts": [gemini_file]}]
            )

            response_diarization = chat_session_diarization.send_message(
                f"Generate meeting diarization of the meeting audio record provided in the file. "
                f"The meeting may be in a foreign language, expect a mixture of words in local language "
                f"and words in english. Provided audio has {num_speakers} speakers. "
                f"Accurately name the speakers or use labels like SPEAKER_01, SPEAKER_02, SPEAKER_03 and so on. "
                f"Provide a structured JSON output. timestamp (hh:mm:ss), speaker (name only), "
                f"speech (transcription). Do not transcribe filler words."
            )

            json_data_diarization = json.loads(response_diarization.text)
            formatted_output = ""
            for item in json_data_diarization:
                formatted_output += f"{item['timestamp']} - {item['speaker']}: {item['speech']}\n\n"

            st.session_state.diarization_output = formatted_output

            # --- Summarization ---
            summarization_config = {
                "temperature": 0.25,
                "top_p": 0.95,
                "top_k": 40,
                "max_output_tokens": 8192,
                "response_schema": content.Schema(
                    type=content.Type.OBJECT,
                    enum=[],
                    required=["summary"],
                    properties={
                        "summary": content.Schema(
                            type=content.Type.STRING,
                        ),
                    },
                ),
                "response_mime_type": "application/json",
            }

            summarization_model = genai.GenerativeModel(
                model_name="gemini-2.0-flash-exp",
                generation_config=summarization_config,
                safety_settings={
                    'HATE': 'BLOCK_NONE',
                    'HARASSMENT': 'BLOCK_NONE',
                    'SEXUAL': 'BLOCK_NONE',
                    'DANGEROUS': 'BLOCK_NONE'
                }
            )

            chat_session_summarization = summarization_model.start_chat(
                history=[{"role": "user", "parts": [st.session_state.diarization_output]}]
            )

            response_summarization = chat_session_summarization.send_message(
                f"Generate a detailed summarization of the meeting, provide information on "
                f"the topic of the meeting, agenda, things discussed and future plans if any mentioned. "
                f"Provide structured output with only one tag 'summary'. Generate response in {language}."
            )

            json_data_summarization = json.loads(response_summarization.text)
            summary = json_data_summarization.get('summary', "No summary found.")
            st.session_state.summary_output = summary
            st.session_state.chat_history.append(("Summary", summary))

            # --- Key Decisions ---
            key_decisions_config = {
                "temperature": 0.25,
                "top_p": 0.95,
                "top_k": 40,
                "max_output_tokens": 8192,
                "response_schema": content.Schema(
                    type=content.Type.OBJECT,
                    enum=[],
                    required=["key_decisions"],
                    properties={
                        "key_decisions": content.Schema(
                            type=content.Type.STRING,
                        ),
                    },
                ),
                "response_mime_type": "application/json",
            }

            key_decisions_model = genai.GenerativeModel(
                model_name="gemini-2.0-flash-exp",
                generation_config=key_decisions_config,
                safety_settings={
                    'HATE': 'BLOCK_NONE',
                    'HARASSMENT': 'BLOCK_NONE',
                    'SEXUAL': 'BLOCK_NONE',
                    'DANGEROUS': 'BLOCK_NONE'
                }
            )

            chat_session_key_decisions = key_decisions_model.start_chat(
                history=[{"role": "user", "parts": [st.session_state.diarization_output]}]
            )

            response_key_decisions = chat_session_key_decisions.send_message(
                f"Identify and list the key decisions made during the meeting. "
                f"Generate response in {language}."
            )

            json_data_key_decisions = json.loads(response_key_decisions.text)
            key_decisions = json_data_key_decisions.get('key_decisions', "No key decisions found.")
            st.session_state.key_decisions_output = key_decisions


        except Exception as e:
            st.error(f"Error processing audio: {str(e)}")
        finally:
            # Clean up temp file if created
            if temp_path:
                os.unlink(temp_path)

# Diarization output display
if st.session_state.diarization_output:
    st.subheader("Diarization Output")
    st.text_area("Transcript", st.session_state.diarization_output, height=300)

# Summary output
if st.session_state.summary_output:
    st.subheader("Summary")
    st.write(st.session_state.summary_output)

# Key decisions output
if st.session_state.key_decisions_output:
    st.subheader("Key decisions")
    key_decisions_list = st.session_state.key_decisions_output.strip().split('\n') # Split by newline
    for decision in key_decisions_list:
        if decision.strip(): # make sure decision is not empty
            st.markdown(f"- {decision.strip()}")


# Generate PDF Report button
if st.button("Generate PDF report"):
    if st.session_state.summary_output and st.session_state.key_decisions_output and st.session_state.diarization_output:
        pdf_bytes = generate_pdf_report(
            meeting_date_time,
            st.session_state.summary_output,
            st.session_state.key_decisions_output,
            st.session_state.diarization_output
        )
        st.download_button(
            label="Download PDF Report",
            data=pdf_bytes,
            file_name="meeting_report.pdf",
            mime="application/pdf"
        )
    else:
        st.warning("Please upload or record audio to generate report.")

# Q&A section
if st.session_state.diarization_output:
    st.subheader("Question Answering")
    question = st.text_input("Type in your question")
    if st.button("Send"):
        if question:
            # Add user question to chat history
            st.session_state.chat_history.append(("User", question))

            with st.spinner("Generating response..."):
                try:
                    # Configure QnA model
                    qna_config = {
                        "temperature": 0.25,
                        "top_p": 0.95,
                        "top_k": 40,
                        "max_output_tokens": 8192,
                        "response_schema": content.Schema(
                            type=content.Type.OBJECT,
                            enum=[],
                            required=["answer"],
                            properties={
                                "answer": content.Schema(
                                    type=content.Type.STRING,
                                ),
                            },
                        ),
                        "response_mime_type": "application/json",
                    }

                    qna_model = genai.GenerativeModel(
                        model_name="gemini-2.0-flash-exp",
                        generation_config=qna_config,
                        safety_settings={
                            'HATE': 'BLOCK_NONE',
                            'HARASSMENT': 'BLOCK_NONE',
                            'SEXUAL': 'BLOCK_NONE',
                            'DANGEROUS': 'BLOCK_NONE'
                        }
                    )

                    # Generate answer
                    chat_session_qna = qna_model.start_chat(
                        history=[{"role": "user", "parts": [st.session_state.diarization_output]}]
                    )

                    response_qna = chat_session_qna.send_message(
                        f"Answer the following question based on the meeting: {question}. Generate response in {language}."
                        f"Provide structured output with only one tag 'answer'."
                    )

                    json_data_qna = json.loads(response_qna.text)
                    answer = json_data_qna.get('answer', "No answer found.")

                    # Add bot response to chat history
                    st.session_state.chat_history.append(("Bot", answer))
                    st.rerun() # Rerun to update the chat display

                except Exception as e:
                    st.error(f"Error generating answer: {str(e)}")

# Chat history display for Q&A
for role, message in st.session_state.chat_history:
    if role == "User":
        st.write(f"**Question**: {message}")
    elif role == "Bot":
        st.write(f"**Answer**: {message}")

# Email input and Send Report button
st.subheader("Share Report")
email_address = st.text_input("Email address:")

send_button = st.button("Send Report")
if send_button:
    if not email_address:
        st.warning("Please enter an email address.")
    elif not (st.session_state.summary_output and st.session_state.key_decisions_output and st.session_state.diarization_output):
        st.warning("Please upload or record audio and generate report first.")
    else:
        success, message = send_email_report(
            email_address,
            meeting_date_time,
            st.session_state.summary_output,
            st.session_state.key_decisions_output,
            st.session_state.diarization_output
        )
        if success:
            st.success(message)
        else:
            st.error(message)