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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
import streamlit as st
import pandas as pd
from groq_llms import LLMHandler
#from openrouter_llms import LLMHandler
import tempfile
import os
from dotenv import load_dotenv

load_dotenv()

# Initialize LLMHandler
llm_handler = LLMHandler()


def process_csv(file, user_prompt):
    """Read CSV, generate responses using LLMHandler, and return processed DataFrame."""
    df = pd.read_csv(file)
    responses = []

    for _, row in df.iterrows():
        try:
            response = llm_handler.generate_response(user_prompt, row.to_dict())
            responses.append(response)
        except Exception as e:
            responses.append(f"Error: {e}")

    df["Generated Text"] = responses
    return df


def initialize_session_state():
    """Initialize session state variables"""
    if 'prompt_creation_method' not in st.session_state:
        st.session_state.prompt_creation_method = None
    if 'current_step' not in st.session_state:
        st.session_state.current_step = 'choose_method'
    if 'context' not in st.session_state:
        st.session_state.context = ""
    if 'questions' not in st.session_state:
        st.session_state.questions = []
    if 'answers' not in st.session_state:
        st.session_state.answers = {}
    if 'multiselect_answers' not in st.session_state:
        st.session_state.multiselect_answers = {}
    if 'custom_options' not in st.session_state:
        st.session_state.custom_options = {}
    if 'final_prompt' not in st.session_state:
        st.session_state.final_prompt = ""
    if 'direct_prompt' not in st.session_state:
        st.session_state.direct_prompt = ""


def display_progress_tracker():
    """Display current progress and previous responses"""
    with st.expander("πŸ“‹ View Progress", expanded=True):
        if st.session_state.prompt_creation_method:
            st.write(f"**Method chosen:** {st.session_state.prompt_creation_method.title()}")

        if st.session_state.context:
            st.write("**Initial Context:**")
            st.info(st.session_state.context)
            if st.button("Edit Context", key="edit_context"):
                st.session_state.current_step = 'initial_context'
                st.rerun()

        if st.session_state.answers:
            st.write("**Your Responses:**")
            for i, question in enumerate(st.session_state.questions):
                if i in st.session_state.multiselect_answers:
                    answers = ", ".join(st.session_state.multiselect_answers[i])
                    st.success(f"Q: {question['question']}\nA: {answers}")
                elif i in st.session_state.answers:
                    st.success(f"Q: {question['question']}\nA: {st.session_state.answers[i]}")
            if st.button("Edit Responses", key="edit_responses"):
                st.session_state.current_step = 'answer_questions'
                st.rerun()

        if st.session_state.direct_prompt:
            st.write("**Your Direct Prompt:**")
            st.info(st.session_state.direct_prompt)
            if st.button("Edit Prompt", key="edit_direct_prompt"):
                st.session_state.current_step = 'direct_prompt'
                st.rerun()

        if st.session_state.final_prompt:
            st.write("**Final Generated Prompt:**")
            st.info(st.session_state.final_prompt)
            if st.button("Edit Final Prompt", key="edit_final_prompt"):
                st.session_state.current_step = 'edit_prompt'
                st.rerun()


# Streamlit UI
st.set_page_config(page_title="Invite AI", page_icon="πŸ’¬", layout="wide")

# Header
st.title("Invite AI")
st.markdown(
    """
    Welcome to the Invitation Generator! This tool helps you create personalized invitations using the power of AI.
    """
)

# Initialize session state
initialize_session_state()

# Display progress tracker (always visible)
display_progress_tracker()

# Sidebar with instructions
st.sidebar.title("Instructions")
st.sidebar.markdown(
    """
    ### Template Download
    [Click here to download the suggested CSV template](http://surl.li/ptvzzv) πŸ“₯
    ### Suggested Requirements
    - **Unique Identifier for each receiver**
    - **Name of the receiver**
    - **Designation/Job title of the receiver**
    - **Company/Organisation where the receiver works**
    - **Areas the receiver is interested in / has expertise in**
    - **Categorize receivers into groups**
    
    [Note: The above template is for your reference, you are free to submit your own data.] 
    """
)

# Main content area with steps
st.markdown("---")  # Separator between progress tracker and current step

if st.session_state.current_step == 'choose_method':
    st.subheader("Choose Your Prompt Creation Method")

    col1, col2 = st.columns(2)

    with col1:
        st.markdown("""
        ### Guided Prompt Builder
        - Step-by-step assistance
        - AI-generated questions
        - Structured approach
        """)
        if st.button("Use Guided Builder"):
            st.session_state.prompt_creation_method = 'guided'
            st.session_state.current_step = 'initial_context'
            st.rerun()

    with col2:
        st.markdown("""
        ### Direct Prompt Entry
        - Write your own prompt
        - Complete control
        - Quick setup
        """)
        if st.button("Use Direct Entry"):
            st.session_state.prompt_creation_method = 'direct'
            st.session_state.current_step = 'direct_prompt'
            st.rerun()

elif st.session_state.current_step == 'direct_prompt':
    st.subheader("Enter Your Prompt")
    st.markdown(
        "Write your complete prompt for generating invitations. Include all necessary details and requirements.")

    direct_prompt = st.text_area(
        "Your Prompt:",
        value=st.session_state.direct_prompt,
        placeholder="Example: Generate a professional invitation for a product launch...",
        height=200
    )

    col1, col2 = st.columns([1, 5])
    with col1:
        if st.button("← Back"):
            st.session_state.current_step = 'choose_method'
            st.rerun()
    with col2:
        if st.button("Continue β†’"):
            if direct_prompt:
                st.session_state.direct_prompt = direct_prompt
                st.session_state.final_prompt = direct_prompt
                st.session_state.current_step = 'upload_process'
                st.rerun()
            else:
                st.error("Please enter a prompt before continuing.")

elif st.session_state.prompt_creation_method == 'guided':
    if st.session_state.current_step == 'initial_context':
        st.subheader("Step 1: Provide Initial Context")
        st.markdown("Briefly describe what your invitation is about (e.g., 'Launching a new GPU product')")

        context = st.text_area(
            "Context:",
            value=st.session_state.context,
            placeholder="Example: Launching a new GPU product for AI and HPC applications",
            height=100
        )

        col1, col2 = st.columns([1, 5])
        with col1:
            if st.button("← Back"):
                st.session_state.current_step = 'choose_method'
                st.rerun()
        with col2:
            if st.button("Generate Questions β†’"):
                if context:
                    st.session_state.context = context
                    st.session_state.questions = llm_handler.generate_questions(context)
                    st.session_state.current_step = 'answer_questions'
                    st.rerun()
                else:
                    st.error("Please provide context before proceeding.")


    # In the answer_questions section of your code, replace the multiselect implementation with this:

    elif st.session_state.current_step == 'answer_questions':

        st.subheader("Step 2: Answer Questions")

        for i, question in enumerate(st.session_state.questions):

            if 'choices' in question:

                # Get previously selected options

                previous_selections = st.session_state.multiselect_answers.get(i, [])

                # Initialize base choices

                base_choices = question['choices'].copy()

                if "Custom" not in base_choices:
                    base_choices.append("Custom")

                # Add any previous custom value to the choices if it exists

                custom_values = [x for x in previous_selections if x not in question['choices'] and x != "Custom"]

                all_choices = base_choices + custom_values

                # Handle word count questions differently

                if any(word in question['question'].lower() for word in ['word count', 'words', 'length']):

                    selected_options = st.multiselect(

                        question['question'],

                        options=all_choices,

                        default=previous_selections,

                        key=f"multiselect_{i}"

                    )

                    if "Custom" in selected_options:

                        # Pre-fill with previous custom value if exists

                        default_custom = next((x for x in previous_selections if x not in base_choices), "")

                        custom_value = st.text_input(

                            "Enter custom word count:",

                            value=default_custom,

                            key=f"custom_{i}"

                        )

                        if custom_value:

                            try:

                                word_count = int(custom_value)

                                if word_count > 0:

                                    selected_options = [opt for opt in selected_options if opt != "Custom"]

                                    if str(word_count) not in selected_options:
                                        selected_options.append(str(word_count))

                                else:

                                    st.error("Please enter a positive number")

                            except ValueError:

                                st.error("Please enter a valid number")


                else:

                    # Regular non-numeric multiselect handling

                    selected_options = st.multiselect(

                        question['question'],

                        options=all_choices,

                        default=previous_selections,

                        key=f"multiselect_{i}"

                    )

                    if "Custom" in selected_options:

                        # Pre-fill with previous custom value if exists

                        default_custom = next((x for x in previous_selections if x not in base_choices), "")

                        custom_value = st.text_input(

                            "Enter your custom response:",

                            value=default_custom,

                            key=f"custom_{i}"

                        )

                        if custom_value:

                            selected_options = [opt for opt in selected_options if opt != "Custom"]

                            if custom_value not in selected_options:
                                selected_options.append(custom_value)

                # Update session state

                st.session_state.multiselect_answers[i] = selected_options

                st.session_state.answers[i] = ", ".join(selected_options) if selected_options else ""


            else:

                # Handle non-choice questions

                st.session_state.answers[i] = st.text_input(

                    question['question'],

                    value=st.session_state.answers.get(i, ""),

                    key=f"question_{i}"

                )

        col1, col2 = st.columns([1, 5])

        with col1:

            if st.button("← Back"):
                st.session_state.current_step = 'initial_context'

                st.rerun()

        with col2:

            if st.button("Generate Prompt β†’"):

                if all(st.session_state.answers.values()):

                    st.session_state.final_prompt = llm_handler.generate_final_prompt(

                        st.session_state.context,

                        st.session_state.questions,

                        st.session_state.answers

                    )

                    st.session_state.current_step = 'edit_prompt'

                    st.rerun()

                else:

                    st.error("Please answer all questions before proceeding.")
    elif st.session_state.current_step == 'edit_prompt':
        st.subheader("Step 3: Review and Edit Final Prompt")
        edited_prompt = st.text_area(
            "Edit your prompt if needed:",
            value=st.session_state.final_prompt,
            height=200
        )

        col1, col2 = st.columns([1, 5])
        with col1:
            if st.button("← Back"):
                st.session_state.current_step = 'answer_questions'
                st.rerun()
        with col2:
            if st.button("Continue to Upload β†’"):
                st.session_state.final_prompt = edited_prompt
                st.session_state.current_step = 'upload_process'
                st.rerun()

# Common upload and processing section for both paths
if st.session_state.current_step == 'upload_process':
    st.subheader("Upload and Process")
    uploaded_file = st.file_uploader("πŸ“‚ Upload CSV File", type=["csv"])

    col1, col2 = st.columns([1, 5])
    with col1:
        if st.button("← Back"):
            if st.session_state.prompt_creation_method == 'guided':
                st.session_state.current_step = 'edit_prompt'
            else:
                st.session_state.current_step = 'direct_prompt'
            st.rerun()

    if uploaded_file is not None and st.session_state.final_prompt:
        st.write("⏳ Processing your file... Please wait.")
        processed_df = process_csv(uploaded_file, st.session_state.final_prompt)

        st.write("### Generated Invitations")
        st.dataframe(processed_df, use_container_width=True)

        with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as temp_file:
            processed_df.to_csv(temp_file.name, index=False)
            temp_file.close()

            st.download_button(
                label="πŸ“₯ Download Results CSV",
                data=open(temp_file.name, "rb"),
                file_name="generated_invitations.csv",
                mime="text/csv",
            )
            os.unlink(temp_file.name)

# Reset button (moved to sidebar)
st.sidebar.markdown("---")
if st.sidebar.button("πŸ”„ Start Over"):
    st.session_state.prompt_creation_method = None
    st.session_state.current_step = 'choose_method'
    st.session_state.context = ""
    st.session_state.questions = []
    st.session_state.answers = {}
    st.session_state.multiselect_answers = {}
    st.session_state.custom_options = {}
    st.session_state.final_prompt = ""
    st.session_state.direct_prompt = ""
    st.rerun()

st.markdown("---")
st.markdown("πŸ’‘ **Tip:** Ensure your data aligns with the provided template for accurate results.")