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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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
import ast
import requests
import json
from duckduckgo_search import DDGS
import google.generativeai as genai
from groq import Groq
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import markdown
import streamlit as st

genai.configure(api_key='AIzaSyCootL_jwKI3YDb6cKRJV-Ad0N4oKlLXXE')
client = Groq(api_key='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')

# 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

def jina(url):
    base_url= "https://r.jina.ai//"
    url=base_url+url
    response=requests.get(url)
    return response.text

def groq_inference(query):
  
  # client = Groq()
  completion = client.chat.completions.create(
      model="llama3-groq-70b-8192-tool-use-preview",
      messages=[
          {
              "role": "user",
              "content": query
          }
      ],
      temperature=0,
      max_tokens=2040,
      top_p=0.65,
      # stream=True,
      stop=None,
  )

  # for chunk in completion:
      # print(chunk.choices[0].delta.content or "", end="")
  # return completion.choices[0].delta.content
  return completion.choices[0].message.content

#Know about product
def serper_prod(company):
  url = "https://google.serper.dev/news"

  payload = json.dumps({
    "q": f"{company} info",
  })
  headers = {
    'X-API-KEY': '7d6a39f71072f99cd421dbdd6cfebc73e2a66a07',
    'Content-Type': 'application/json'
  }

  response = requests.request("POST", url, headers=headers, data=payload)

  return json.loads(response.text)


#Know about the competiton\competitiors
def serper_compi(company):
  url = "https://google.serper.dev/search"

  payload = json.dumps({
    "q": f"{company} competitors.",
  })
  headers = {
    'X-API-KEY': '7d6a39f71072f99cd421dbdd6cfebc73e2a66a07',
    'Content-Type': 'application/json'
  }

  response = requests.request("POST", url, headers=headers, data=payload)

  return json.loads(response.text)

def AI_Search_compi(text):
  ans = DDGS().chat("Summarize the text and dont remove the important terms about products or applications which should be helped for planning market for a company " + text, model='claude-3-haiku')
  return ans

def AI_Search_compi(text,titles,name):
  ans = groq_inference(f"""Summarize the text of the articles titles are {titles} and dont remove the important terms about products or applications which should be helped for knowing about the compititors for a company {name} and the data is: {text}""")
  return ans
 
def AI_Product_Analysis(text):
    ans = DDGS().chat("Analyze the products mentioned in the following news article in 400 words or fewer. Focus on their features, market relevance, and potential impact for a company's market planning: " + text, model='claude-3-haiku')
    return ans

def AI_Product_Summary(news_summaries,product):
    # combined_summaries = " ".join(news_summaries)
    ans = groq_inference(f"""Create a comprehensive summary of the product {product} based on the following summaries of 10 or fewer news articles. Ensure no important product details are lost and remember that these are form the news articles so you may have add text also : """ + news_summaries)
    # ans = DDGS().chat("Create a comprehensive summary of the product based on the following summaries of 10 or fewer news articles. Ensure no important product details are lost: " + news_summaries, model='gpt-4o-mini')
    return ans

def AI_Product_Summary_prod(news_summaries):
    # combined_summaries = " ".join(news_summaries)
    ans = groq_inference(f"""Create a comprehensive summary of the product based on the following summaries of 10 or fewer news articles. Ensure no important product details are lost and remember that these are form the news articles so you may have add text also : """ + news_summaries)
    # ans = DDGS().chat("Create a comprehensive summary of the product based on the following summaries of 10 or fewer news articles. Ensure no important product details are lost: " + news_summaries, model='gpt-4o-mini')
    return ans
# AI_Product_Summary_prod

def AI_Search_extract_cmpy(text):
#   prompt = """You will be given a dynamic text that summarizes key products, applications, and comparisons from articles about VR headsets. Your task is to extract relevant product names from the text and generate a list of search queries suitable for a search API.Don't give more than five names in the list.

# Output Format:

# The output should be a list in the following format:
# ['company - product name', 'company - product name', 'product name', ...]
# If the company name is unknown, only include the product name without the company name.
# Do not include any introductory or explanatory text in the output; provide only the list in brackets.
# Input Format:

# The input will be a summary text containing product names, features, and key points."""
#$#$#$#$#
  prompt = """You will be given a dynamic text that summarizes key products, applications, and comparisons from articles about VR headsets. Your task is to extract relevant product names from the text and generate a list of search queries suitable for a search API. Don't give more than five names in the list.
  Output Format:

Provide only the list in the following format, without any explanatory or introductory text:
['company - product name', 'company - product name', 'product name', ...]
and remember dont give like this ['McDonald's - Big Mac'] if the company is like this then give it as ['McDonalds - Big Mac'] or ['Dunkin's Donut'] to ['Dunkins Donut'] etc, becauses this will be given to pyhton this return error.
If the company name is unknown, only include the product name without the company name."""
#   prompt = """You will be given a dynamic text that summarizes key products, applications, and comparisons from articles about VR headsets. Your task is to extract relevant product names from the text and generate a list of search queries suitable for a search API. Don't give more than five names in the list.Dont repeat the company names and please dont add single quotes or any other special characters that will be given error for pyhton to read.
#   Output Format:

# Provide only the list in the following format, without any explanatory or introductory text:
# ['company - product name', 'company - product name', 'product name', ...]
# and remember dont give like this ['McDonald's - Big Mac'] becauses this will be given to pyhton this return error.
# If the company name is unknown, only include the product name without the company name."""

#   ans = groq_inference(prompt + f"the text is: {text}")
  ans = DDGS().chat(prompt+"the text is \n" + text, model='claude-3-haiku')
  return ast.literal_eval(ans)


def AI_Company_Summary(news_summaries):
    ans = DDGS().chat("Analyze the following combined summaries about multiple products. Provide a detailed summary of each product individually, clearly outlining their features, market relevance, and competitive advantages, so this information can be used to analyze competitor products: " + news_summaries, model='claude-3-haiku')
    return ans

def AI_Analysis(Product_analysis,Compitetiors_analysis):
    prompt = Compitetiors_analysis + "Product Information: \n " + Product_analysis
    system_prompt =  "Analyze the following competitor and product details. Provide a thorough technical analysis of each product, focusing on its market standing, technical strengths, and areas for improvement. Offer actionable insights on how the product can be enhanced to increase sales and profitability. Compare the product with competitors, identifying gaps and opportunities for differentiation and market leadership and remember that give the analyis of the compitiors only if they are related else dont give it: " + "Competitors: \n "+ prompt,

    # ans = DDGS().chat(prompt, model='claude-3-haiku')
    # if len(prompt) > 22000:
    #     prompt = prompt[:22000]
    # ans = DDGS().chat(
    # "Analyze the following competitor and product details. Provide a thorough technical analysis of each product, focusing on its market standing, technical strengths, and areas for improvement. Offer actionable insights on how the product can be enhanced to increase sales and profitability. Compare the product with competitors, identifying gaps and opportunities for differentiation and market leadership: " + "Competitors: \n "
    # + prompt,
    # model='claude-3-haiku')
    model = genai.GenerativeModel(model_name="gemini-1.5-flash")
    response = model.generate_content(system_prompt)

    return response.text

def AI_Analysis(Product_analysis,Compitetiors_analysis):
    prompt = Compitetiors_analysis + "Product Information: \n " + Product_analysis
    system_prompt =  "Analyze the following competitor and product details. Provide a thorough technical analysis of each product, focusing on its market standing, technical strengths, and areas for improvement. Offer actionable insights on how the product can be enhanced to increase sales and profitability. Compare the product with competitors, identifying gaps and opportunities for differentiation and market leadership and remember that give the analyis of the compitiors only if they are related else dont give it: " + "Competitors: \n "+ prompt,

    # ans = DDGS().chat(prompt, model='claude-3-haiku')
    # if len(prompt) > 22000:
    #     prompt = prompt[:22000]
    # ans = DDGS().chat(
    # "Analyze the following competitor and product details. Provide a thorough technical analysis of each product, focusing on its market standing, technical strengths, and areas for improvement. Offer actionable insights on how the product can be enhanced to increase sales and profitability. Compare the product with competitors, identifying gaps and opportunities for differentiation and market leadership: " + "Competitors: \n "
    # + prompt,
    # model='claude-3-haiku')
    model = genai.GenerativeModel(model_name="gemini-1.5-flash")
    response = model.generate_content(system_prompt)

    return response.text

# """ This takes input of company and return summary of the company"""
def analysis_name(text):
  compi_urls = [i['link'] for i in text['news']][0:4]
  print(compi_urls)
  text = [jina(url) for url in compi_urls]
  ans = ' '.join(AI_Product_Analysis(i[:8000]) for i in text if len(i)>1500)
  # time.sleep(25)
  summ = AI_Product_Summary_prod(ans[:21000])
  return summ


# """" This is for knowing about the Product"""
# 11
# groq
def know_prod(name):
  ser = serper_prod(name)
  compi_urls = [i['link'] for i in ser['news']]
  text = [jina(url) for url in compi_urls]
  ans = ' '.join(AI_Product_Analysis(i[:8000]) for i in text if len(i)>1500)
  summ = AI_Product_Summary(ans[:20000],name)
  return summ

# """ This takes input of company and return list of the compi"""
# 11
def compi_main(name):
  ret = serper_compi(name)
  compi_urls = [i['link'] for i in ret['organic']]
  title_list= [i['title'] for i in ret['organic']]
  text = [jina(url) for url in compi_urls]
  ans = ' '.join(AI_Search_compi(i[:8000],title_list,name) for i in text if len(i)>1500)
  lst = AI_Search_extract_cmpy(ans)
  return lst

# """ This takes input of company list and return summary of the company"""
#21
def summary_name(otp):
  names=[]
  title=otp
  for idx, i in enumerate(otp):
      results = serper_prod(i)  # Call your function with the company name
      globals()[f'compi_{chr(65 + idx)}'] = results
      # print(i)
      # print(f'compi_{chr(65 + idx)}')
      names.append(f'compi_{chr(65 + idx)}')
  text_data = [globals()[name] for name in names]
  summ_data = [analysis_name(i) for i in text_data]
  cmpy_summ = AI_Company_Summary(" ".join(summ_data))
  return cmpy_summ

# """ This takes inputs of compi summary and product summary and return summary of the company"""
#1
def analysis_prod(prod_summ,cmpy_summ):
  return AI_Analysis(prod_summ,cmpy_summ)


# def main(name,email_id):
#   start_time = time.time()
#   prod_info = know_prod(name)
#   end_prd_anal = time.time()
#   print(prod_info)
#   print(f"Time taken to analyze the product: {end_prd_anal - start_time} seconds")
#   print('*****************************************************************')
#   # print(prod_info)
#   print(len(prod_info))
#   otp = compi_main(name)
#   end_compi = time.time()
#   print(len(otp))
#   print(f"Time taken to analyze the competitors: {end_compi - end_prd_anal} seconds")
#   print('*****************************************************************')
#   # print(otp)
#   summpop = summary_name(otp)
#   print(len(summpop))
#   end_time_summary = time.time()
#   print(f"Time taken to analyze the summary: {end_time_summary - end_compi} seconds")
#   print('*****************************************************************')
#   # print(summpop)
#   print('*****************************************************************')
#   analysis_total = analysis_prod(prod_info,summpop)
#   end_time = time.time()
#   print(f"Analysis time taken: {end_time - end_time_summary} seconds")
#   print('*****************************************************************')
#   print(f"Total time taken: {end_time - start_time} seconds")
#   print('*****************************************************************')
#   # print(analysis(prod_info,summpop))
#   #Send emails
#   send_email_gmail(email_id,analysis_total)


#   return start_time, end_prd_anal, end_compi, end_time_summary , end_time, analysis_total

def time_calculator(start_time, end_time):
    time_in_seconds = end_time - start_time
    minutes = int(time_in_seconds // 60)  # Get minutes
    seconds = int(time_in_seconds % 60) 
    time_taken = f"{minutes} minutes and {seconds} seconds"
    return time_taken

def send_email_gmail(receiver_email,markdown_content):
    sender_email = "srishnotebooks@gmail.com"
    sender_password = "zoge jatp yaib qtsz"# replace with the app password generated
    # receiver_email = "recipient_email@example.com"
    subject = "Product Analysis Report"

    # Create the email message container
    msg = MIMEMultipart('alternative')
    msg['From'] = sender_email
    msg['To'] = receiver_email
    msg['Subject'] = subject

    # Convert markdown to HTML
    html_content = markdown.markdown(markdown_content)

    # Attach the HTML content
    msg.attach(MIMEText(html_content, 'html'))

    try:
        # Set up the server using Gmail's SMTP
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()  # Encrypt the connection
        server.login(sender_email, sender_password)  # Use App Password instead of Gmail password
        
        # Send the email
        server.sendmail(sender_email, receiver_email, msg.as_string())
        server.quit()

        print("Email sent successfully!")
        # return True
    except Exception as e:
        print(f"Error sending email: {str(e)}")
        # return False

# def main(name, email_id):
#     start_time = time.time()
    
#     # Display the process in Streamlit
#     st.write("Analyzing product information...")
#     prod_info = know_prod(name)
#     end_prd_anal = time.time()
    
#     # Show product info and analysis time
#     st.write("Product Information:")
#     st.write(prod_info)
#     st.write(f"Time taken to analyze the product: {end_prd_anal - start_time} seconds")
    
#     st.write('*****************************************************************')
#     st.write(f"Number of websites analyzed: {len(prod_info)}")
    
#     # Competitor analysis
#     st.write("Analyzing competitors...")
#     otp = compi_main(name)
#     end_compi = time.time()
    
#     st.write(f"Number of competitors found: {len(otp)}")
#     st.write(f"Time taken to analyze the competitors: {end_compi - end_prd_anal} seconds")
    
#     st.write('*****************************************************************')
    
#     # Summary analysis
#     st.write("Generating summary...")
#     summpop = summary_name(otp)
#     end_time_summary = time.time()
    
#     # st.write('## Compititors Summary')
#     # st.write(f"len(summpop)")
#     st.write(f"Time taken to generate summary: {end_time_summary - end_compi} seconds")
    
#     st.write('*****************************************************************')
    
#     # Total analysis
#     analysis_total = analysis_prod(prod_info, summpop)
#     end_time = time.time()
    
#     st.write("## Total Analysis")
#     st.write(analysis_total)
#     # st.write(f"Analysis time taken: {end_time - end_time_summary} seconds")
#     # st.write(f"Total time taken: {end_time - start_time} seconds")
    
#     st.write('*****************************************************************')
    
#     # Send email
#     send_email_gmail(email_id, analysis_total)
    
#     return start_time, end_prd_anal, end_compi, end_time_summary, end_time, analysis_total
# def main(name, email_id):
#     start_time = time.time()

#     # Use container to group elements in a card-like style
#     with st.container():
#         st.markdown('---')  # Horizontal line before the section

#         with st.spinner("Analyzing product information..."):
#             prod_info = know_prod(name)
#         end_prd_anal = time.time()

#         # Show product info and analysis time
#         st.write("### Product Information:")
#         st.write(prod_info)
#         st.write(f"Time taken to analyze the product: {end_prd_anal - start_time:.2f} seconds")
#         st.write(f"Number of websites analyzed: {len(prod_info)}")
    
#         st.markdown('---')  # Horizontal line after the section

#     with st.container():
#         st.markdown('---')  # Horizontal line before the section

#         with st.spinner("Analyzing competitors..."):
#             otp = compi_main(name)
#         end_compi = time.time()

#         st.write("### Competitor Analysis:")
#         st.write(f"Number of competitors found: {len(otp)}")
#         st.write(f"Time taken to analyze the competitors: {end_compi - end_prd_anal:.2f} seconds")
    
#         st.markdown('---')  # Horizontal line after the section

#     with st.container():
#         st.markdown('---')  # Horizontal line before the section

#         with st.spinner("Generating summary..."):
#             summpop = summary_name(otp)
#         end_time_summary = time.time()

#         st.write("### Summary Analysis:")
#         st.write(f"Time taken to generate summary: {end_time_summary - end_compi:.2f} seconds")

#         st.markdown('---')  # Horizontal line after the section

#     with st.container():
#         st.markdown('---')  # Horizontal line before the section

#         st.write("### Total Analysis:")
#         analysis_total = analysis_prod(prod_info, summpop)
#         st.write(analysis_total)

#         end_time = time.time()

#         st.write(f"Analysis time taken: {end_time - end_time_summary:.2f} seconds")
#         st.write(f"Total time taken: {end_time - start_time:.2f} seconds")

#         st.markdown('---')  # Horizontal line after the section

#     # Email handling
#     if not email_id:
#         st.error("Email is not sent because it was not provided.", icon="🚫")
#     else:
#         # Call send_email_gmail and check if email is sent
#         if send_email_gmail(email_id, analysis_total):
#             st.success("Email sent successfully!", icon="βœ…")
#         else:
#             st.error("Failed to send email.", icon="❌")

#     return start_time, end_prd_anal, end_compi, end_time_summary, end_time, analysis_total


# # Streamlit UI
# st.title("Product and Competitor Analysis")

# # Inputs from user
# name = st.text_input("Enter the Product Name", "Cafe Coffee Day")
# email_id = st.text_input("Enter your Email", "kapishrachamalla32@gmail.com")

# if st.button("Start Analysis"):
#     t1, t2, t3, t4, t5, analysis = main(name, email_id)
    
#     # Time breakdown in minutes
#     st.write("Time taken to know about the product:", (t2 - t1) / 60, "minutes")
#     st.write("Time taken to know about the competitors:", (t3 - t2) / 60, "minutes")
#     st.write("Time taken to give analysis:", (t4 - t3) / 60, "minutes")
#     st.write("Time taken to generate summary:", (t5 - t4) / 60, "minutes")
#     st.write("Total time taken:", (t5 - t1) / 60, "minutes")
#$%$%$

def colored_container(color, content):
    st.markdown(
        f"""
        <div style="background-color: {color}; padding: 10px; border-radius: 5px;">
        {content}
        </div>
        """, unsafe_allow_html=True
    )

def main(name, email_id):
    
    start_time = time.time()
#     colored_container("#A9DFBF", f"""
# <h4 style="color: black;">Total Analysis:</h4>
# <p style="color: black;">## Technical Analysis of McDonald's Chicken Big Mac</p>
# <p style="color: black;"><strong>Market Standing:</strong><br>The Chicken Big Mac represents a strategic move by McDonald's to tap into the growing demand for chicken-based menu items in the fast-food industry. This trend is driven by consumer interest in healthier options, a wider variety of protein sources, and the growing popularity of chicken sandwiches in general.</p>
# <p style="color: black;"><strong>Technical Strengths:</strong></p>
# <ul style="color: black;">
#     <li>Leveraging Existing Brand Equity: The Chicken Big Mac benefits from the strong brand equity of the iconic Big Mac, ensuring immediate recognition and consumer interest.</li>
#     <li>Innovation: The use of tempura-battered chicken patties represents an innovative approach to chicken preparation, adding a unique flavor profile and appealing to a broader customer base.</li>
#     <li>Meeting Consumer Preferences: The sandwich addresses the growing demand for chicken-based options, demonstrating McDonald's ability to adapt to changing consumer preferences.</li>
# </ul>
# <p style="color: black;"><strong>Areas for Improvement:</strong></p>
# <ul style="color: black;">
#     <li>Product Differentiation: While the Chicken Big Mac leverages the Big Mac's brand equity, it might benefit from more distinct features to differentiate itself further from other chicken sandwiches in the market.</li>
#     <li>Nutritional Profile: The tempura batter might raise concerns about the nutritional profile of the sandwich, potentially impacting its appeal to health-conscious consumers.</li>
#     <li>Marketing and Promotion: McDonald's needs to develop a comprehensive marketing strategy to effectively promote the Chicken Big Mac and highlight its unique selling points.</li>
# </ul>
# <p style="color: black;"><strong>Actionable Insights:</strong></p>
# <ul style="color: black;">
#     <li>Enhance Differentiation: Consider adding unique ingredients or flavor profiles to further distinguish the Chicken Big Mac from other offerings.</li>
#     <li>Promote Healthier Options: Explore lighter batter options or create a "healthier" version of the Chicken Big Mac with grilled chicken and lighter sauces.</li>
#     <li>Targeted Marketing: Focus marketing efforts on highlighting the innovation and appeal of the Chicken Big Mac, reaching target demographics interested in chicken-based options.</li>
# </ul>
# <p style="color: black;"><strong>Comparison to Competitors:</strong></p>
# <p style="color: black;">McDonald's needs to analyze the competitive landscape of chicken sandwiches. This includes identifying key competitors like Chick-fil-A, Wendy's, and Popeyes, and comparing their offerings in terms of flavor profiles, ingredients, and marketing strategies. This analysis will help McDonald's identify potential gaps and opportunities for differentiation.</p>
# <p style="color: black;"><strong>Opportunities for Market Leadership:</strong></p>
# <ul style="color: black;">
#     <li>Focus on Premium Quality: McDonald's can leverage its brand reputation to introduce a premium chicken sandwich with higher-quality ingredients and a unique flavor profile.</li>
#     <li>Create a Signature Chicken Experience: Develop a distinctive chicken sandwich experience that sets it apart from competitors, emphasizing its unique taste and texture.</li>
#     <li>Promote Chicken Innovation: Leverage the Chicken Big Mac's launch to position McDonald's as a leader in chicken innovation, showcasing a commitment to meeting evolving consumer demands.</li>
# </ul>
# <p style="color: black;"><strong>Conclusion:</strong></p>
# <p style="color: black;">The Chicken Big Mac holds significant potential for McDonald's to expand its market share in the growing chicken sandwich segment. By addressing its weaknesses, leveraging its strengths, and actively monitoring competitive offerings, McDonald's can create a successful product that drives sales and profitability.</p>
# <p style="color: black;">Analysis time taken: 4.51 seconds</p>
# <p style="color: black;">Total time taken: 368.86 seconds</p>
# """)

    # Product information section
    with st.container():
        st.markdown('---')  # Horizontal line before the section

        with st.spinner("Analyzing product information..."):
            prod_info = know_prod(name)
        end_prd_anal = time.time()

        # Display product info in a colored container
        # colored_container("#D6EAF8", f"""
        # <h4>Product Information:</h4>
        # <p>{prod_info}</p>
        # <p>Time taken to analyze the product: {end_prd_anal - start_time:.2f} seconds</p>
        # <p>Number of websites analyzed: {len(prod_info)}</p>
        # """)

        st.write("### Product Information:")
        st.write(prod_info)
        # st.write(f"Time taken to analyze the product: {end_prd_anal - start_time:.2f} seconds")
        st.write(f"Number of Articles analyzed: {len(prod_info)}")
        st.markdown('---')  # Horizontal line after the section

    # Competitor analysis section
    with st.container():
        st.markdown('---')  # Horizontal line before the section

        with st.spinner("Analyzing competitors..."):
            otp = compi_main(name)
        end_compi = time.time()

        # Display competitor info in a different colored container
        # colored_container("#F9E79F", f"""
        # <h4>Competitor Analysis:</h4>
        # <p>Number of competitors found: {len(otp)}</p>
        # <p>Time taken to analyze the competitors: {end_compi - end_prd_anal:.2f} seconds</p>
        # """)

        st.write("### Competitor Analysis:")
        st.write(f"Number of potential competitors found: {len(otp)}")
        # st.write(f"The Competitors are: \n {otp}")
        # st.write(f"Time taken to analyze the competitors: {end_compi - end_prd_anal:.2f} seconds")

        # st.markdown('---')  # Horizontal line after the section

    # Summary analysis section
    # with st.container():
        # st.markdown('---')  # Horizontal line before the section

        with st.spinner("Generating summary..."):
            summpop = summary_name(otp)
        end_time_summary = time.time()

        # Display summary in a different color container
        # colored_container("#A9DFBF", f"""
        # <h4>Summary Analysis:</h4>
        # <p>Time taken to generate summary: {end_time_summary - end_compi:.2f} seconds</p>
        # """)

        st.write("### Summary Analysis:")
        st.write(summpop)
        # st.write(f"Time taken to generate summary: {end_time_summary - end_compi:.2f} seconds")

        st.markdown('---')  # Horizontal line after the section

    # Total analysis section
    with st.container():
        st.markdown('---')  # Horizontal line before the section

        # Display total analysis in another color container
        analysis_total = analysis_prod(prod_info, summpop)
        end_time = time.time()
        # colored_container("#F5B7B1", f"""
        # <h4>Total Analysis:</h4>
        # <p>{analysis_total}</p>
        # <p>Analysis time taken: {end_time - end_time_summary:.2f} seconds</p>
        # <p>Total time taken: {end_time - start_time:.2f} seconds</p>
        # """)

        # st.write("### Total Analysis:")
        st.write(analysis_total)

        st.markdown('---')  # Horizontal line after the section

    # Email handling
    if not email_id:
        st.error("Email is not sent because it was not provided.", icon="🚫")
    else:
        # Call send_email_gmail and check if email is sent
        send_email_gmail(email_id, analysis_total)
        st.success("Email sent successfully!", icon="βœ…")
        # else:
            # st.error("Failed to send email.", icon="❌")

    return start_time, end_prd_anal, end_compi, end_time_summary, end_time, analysis_total


# Streamlit UI
st.title("Market Mind 🧠")
# st.subheader("Empowering You with ")
st.markdown("<h7>Real-Time Market Intelligence</h1>", unsafe_allow_html=True)
 # Sidebar for developer profiles and hackathon info
st.sidebar.markdown(
        """
        <h1 style='color: #ff0000;'>πŸš€ Hackathon Project</h1>
        """, 
        unsafe_allow_html=True
    )
st.sidebar.markdown("Welcome to the MarketMind project, developed for the hackathon to showcase AI power in the product and competitor analysis. πŸš€")

    # Add some icons/emojis to make it look more engaging
st.sidebar.markdown("### πŸ”§ Project Features")
    # st.sidebar.markdown("- Analyze product details using OpenFoodFacts API.")
st.sidebar.markdown("- Real-Time Market Intelligence: Offers real-time data updates for informed decision-making")
st.sidebar.markdown("- AI and Machine Learning: Helps analyze competitors and suggests improvement strategies based on data.")

    # Developer details with LinkedIn links
st.sidebar.markdown("### πŸ‘¨β€πŸ’» Developers")
st.sidebar.markdown("[Srish](https://www.linkedin.com/in/srishrachamalla/) - AI/ML Developer")
st.sidebar.markdown("[Sai Teja](https://www.linkedin.com/in/saiteja-pallerla-668734225/) - Data Analyst")

    # Add expander sections for additional content
with st.sidebar.expander("β„Ή About MarketMind"):
    st.write("MarketMind is a platform focused on providing advanced data analytics, market intelligence, and AI-driven insights for businesses, investors, and market professionals. Its solutions are aimed at helping organizations make informed decisions by analyzing vast amounts of market data, consumer behavior, and industry trends in real-time")

with st.sidebar.expander("πŸ“š Useful Resources"):
    st.write("[Google Gemini AI Documentation](https://ai.google.dev/gemini-api/docs)")
    st.write("[Streamlit Documentation](https://docs.streamlit.io/)")
    st.write("[Groq Documentation](https://console.groq.com/docs/quickstart)")

    # Add progress indicator for hackathon phases or development stages
st.sidebar.markdown("### ⏳ Hackathon Progress")
st.sidebar.progress(0.99)  # Set progress level (0 to 1)

    # Sidebar footer with final notes
st.sidebar.markdown("---")
st.sidebar.markdown(
        """
        <div style="text-align: center; font-size: 0.85em;">
            Developed by Srish & Sai Teja β€’ Powered by Google Gemini AI
        </div>
        """, unsafe_allow_html=True
    )
# Inputs from user
name = st.text_input("Enter the Product Name", "Cafe Coffee Day")
email_id = st.text_input("Enter your Email", "")

if st.button("Start Analysis"):
    t1, t2, t3, t4, t5, analysis = main(name, email_id)
    
    # # Time breakdown in minutes
    # st.write("### Time Breakdown (in minutes)")
    # st.write(f"Time taken to analyze the product: {(t2 - t1) / 60:.2f} minutes")
    # st.write(f"Time taken to analyze competitors: {(t3 - t2) / 60:.2f} minutes")
    # st.write(f"Time taken to generate summary: {(t4 - t3) / 60:.2f} minutes")
    # st.write(f"Time taken for total analysis: {(t5 - t1) / 60:.2f} minutes")
    # Time breakdown in minutes
    st.write("### Time Breakdown (in minutes)")

    # Define colors for each breakdown
    total_color = "#FF5733"  # Red
    competitors_color = "#33C1FF"  # Blue
    summary_color = "#75FF33"  # Green
    product_color = "#FF33B5"  # Pink

    # Display each time taken in different colors
     # Get seconds
    st.markdown(f"<p style='color: {product_color};'>Time taken to analyze the product: {time_calculator(t2 , t1)}</p>", unsafe_allow_html=True)
    st.markdown(f"<p style='color: {competitors_color};'>Time taken to analyze competitors: {time_calculator(t3 , t2)} minutes</p>", unsafe_allow_html=True)
    st.markdown(f"<p style='color: {summary_color};'>Time taken to generate summary: {time_calculator(t4 , t3)} minutes</p>", unsafe_allow_html=True)
    st.markdown(f"<p style='color: {total_color};'>Time taken for total analysis: {time_calculator(t5 , t1)} minutes</p>", unsafe_allow_html=True)
    st.markdown("---")
st.markdown("""
        <div style="text-align: center; font-size: 0.9em;">
        <p><i>MarketMind</i> was developed for a hackathon using <b>Streamlit</b> to showcase AI power in product and competitor analysis.</p>
        <p>Developed by Srish & Sai Teja </p>
        </div>
        """, unsafe_allow_html=True)
# if __name__ == "__main__":
#     t1,t2,t3,t4,t5,analysis = main('Cafe Coffee Day',"kapishrachamalla32@gmail.com")
#     print("time taken to Know about the product: ", (t2-t1)/60)
#     print("time taken to Know about the competitors: ", (t3-t2)/60)
#     print("time taken to Know about the give analysis: ", (t4-t3)/60)
#     print("time taken to Know about the summary: ", (t5-t4)/60)
#     print("total time taken: ", (t5-t1)/60)


# import ast
# import requests
# import json
# import time
# import streamlit as st
# from duckduckgo_search import DDGS
# import google.generativeai as genai
# from groq import Groq
# import smtplib
# from email.mime.text import MIMEText
# from email.mime.multipart import MIMEMultipart
# import markdown

# # Configure generative AI API key
# genai.configure(api_key='AIzaSyCootL_jwKI3YDb6cKRJV-Ad0N4oKlLXXE')
# client = Groq(api_key='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')

# # Function definitions as in your original code

# def jina(url):
#     base_url= "https://r.jina.ai//"
#     url=base_url+url
#     response=requests.get(url)
#     return response.text

# def groq_inference(query):
#     completion = client.chat.completions.create(
#         model="llama3-groq-70b-8192-tool-use-preview",
#         messages=[{"role": "user", "content": query}],
#         temperature=0,
#         max_tokens=2040,
#         top_p=0.65,
#         stop=None,
#     )
#     return completion.choices[0].message.content

# def serper_prod(company):
#     url = "https://google.serper.dev/news"
#     payload = json.dumps({"q": f"{company} info"})
#     headers = {
#         'X-API-KEY': '7d6a39f71072f99cd421dbdd6cfebc73e2a66a07',
#         'Content-Type': 'application/json'
#     }
#     response = requests.request("POST", url, headers=headers, data=payload)
#     return json.loads(response.text)

# def serper_compi(company):
#     url = "https://google.serper.dev/search"
#     payload = json.dumps({"q": f"{company} competitors."})
#     headers = {
#         'X-API-KEY': '7d6a39f71072f99cd421dbdd6cfebc73e2a66a07',
#         'Content-Type': 'application/json'
#     }
#     response = requests.request("POST", url, headers=headers, data=payload)
#     return json.loads(response.text)

# def AI_Search_compi(text, titles, name):
#     ans = groq_inference(f"""Summarize the text of the articles titles are {titles} and don't remove the important terms about products or applications which should help in knowing about the competitors for a company {name}. The data is: {text}""")
#     return ans

# def AI_Product_Analysis(text):
#     ans = DDGS().chat("Analyze the products mentioned in the following news article in 400 words or fewer. Focus on their features, market relevance, and potential impact for a company's market planning: " + text, model='claude-3-haiku')
#     return ans

# def AI_Product_Summary(news_summaries, product):
#     ans = groq_inference(f"""Create a comprehensive summary of the product {product} based on the following summaries of 10 or fewer news articles. Ensure no important product details are lost, and you may add text from the articles as well: {news_summaries}""")
#     return ans

# def AI_Search_extract_cmpy(text):
#     prompt = """You will be given a dynamic text that summarizes key products, applications, and comparisons from articles. Your task is to extract relevant product names from the text and generate a list of search queries suitable for a search API. Don't give more than five names in the list. Output Format: ['company - product name', 'company - product name', 'product name', ...]."""
#     ans = DDGS().chat(prompt + "The text is \n" + text, model='claude-3-haiku')
#     print(ans)
#     return ast.literal_eval(ans)

# def AI_Company_Summary(news_summaries):
#     ans = DDGS().chat("Analyze the following combined summaries about multiple products. Provide a detailed summary of each product individually, clearly outlining their features, market relevance, and competitive advantages: " + news_summaries, model='claude-3-haiku')
#     return ans

# def AI_Product_Summary_prod(news_summaries):
#     # combined_summaries = " ".join(news_summaries)
#     ans = groq_inference(f"""Create a comprehensive summary of the product based on the following summaries of 10 or fewer news articles. Ensure no important product details are lost and remember that these are form the news articles so you may have add text also : """ + news_summaries)
#     # ans = DDGS().chat("Create a comprehensive summary of the product based on the following summaries of 10 or fewer news articles. Ensure no important product details are lost: " + news_summaries, model='gpt-4o-mini')

# def AI_Analysis(Product_analysis, Compitetiors_analysis):
#     prompt = Compitetiors_analysis + "Product Information: \n " + Product_analysis
#     system_prompt = f"""Analyze the following competitor and product details. Provide a technical analysis of each product, focusing on its market standing, technical strengths, and areas for improvement. Compare the product with competitors, identifying gaps and opportunities for differentiation: Competitors: {prompt}"""
    
#     model = genai.GenerativeModel(model_name="gemini-1.5-flash")
#     response = model.generate_content(system_prompt)
#     return response.text

# def send_email_gmail(receiver_email,markdown_content):
#     sender_email = "srishnotebooks@gmail.com"
#     sender_password = "zoge jatp yaib qtsz"# replace with the app password generated
#     # receiver_email = "recipient_email@example.com"
#     subject = "Product Analysis Report"

#     # Create the email message container
#     msg = MIMEMultipart('alternative')
#     msg['From'] = sender_email
#     msg['To'] = receiver_email
#     msg['Subject'] = subject

#     # Convert markdown to HTML
#     html_content = markdown.markdown(markdown_content)

#     # Attach the HTML content
#     msg.attach(MIMEText(html_content, 'html'))

#     try:
#         # Set up the server using Gmail's SMTP
#         server = smtplib.SMTP('smtp.gmail.com', 587)
#         server.starttls()  # Encrypt the connection
#         server.login(sender_email, sender_password)  # Use App Password instead of Gmail password
        
#         # Send the email
#         server.sendmail(sender_email, receiver_email, msg.as_string())
#         server.quit()

#         print("Email sent successfully!")
#     except Exception as e:
#         print(f"Error sending email: {str(e)}")

# # """ This takes input of company and return summary of the company"""
# def analysis_name(text):
#   compi_urls = [i['link'] for i in text['news']][0:4]
#   print(compi_urls)
#   text = [jina(url) for url in compi_urls]
#   ans = ' '.join(AI_Product_Analysis(i[:8000]) for i in text if len(i)>1500)
#   # time.sleep(25)
#   summ = AI_Product_Summary_prod(ans[:21000])
#   return summ

# # """" This is for knowing about the Product"""
# # 11
# # groq
# def know_prod(name):
#   ser = serper_prod(name)
#   compi_urls = [i['link'] for i in ser['news']]
#   text = [jina(url) for url in compi_urls]
#   ans = ' '.join(AI_Product_Analysis(i[:8000]) for i in text if len(i)>1500)
#   summ = AI_Product_Summary(ans[:20000],name)
#   return summ

# # """ This takes input of company and return list of the compi"""
# # 11
# def compi_main(name):
#   ret = serper_compi(name)
#   compi_urls = [i['link'] for i in ret['organic']]
#   title_list= [i['title'] for i in ret['organic']]
#   text = [jina(url) for url in compi_urls]
#   ans = ' '.join(AI_Search_compi(i[:8000],title_list,name) for i in text if len(i)>1500)
#   lst = AI_Search_extract_cmpy(ans)
#   return lst

# # """ This takes input of company list and return summary of the company"""
# #21
# def summary_name(otp):
#   names=[]
#   title=otp
#   for idx, i in enumerate(otp):
#       results = serper_prod(i)  # Call your function with the company name
#       globals()[f'compi_{chr(65 + idx)}'] = results
#       # print(i)
#       # print(f'compi_{chr(65 + idx)}')
#       names.append(f'compi_{chr(65 + idx)}')
#   text_data = [globals()[name] for name in names]
#   summ_data = [analysis_name(i) for i in text_data]
#   cmpy_summ = AI_Company_Summary(" ".join(summ_data))
#   return cmpy_summ

# # """ This takes inputs of compi summary and product summary and return summary of the company"""
# #1
# def analysis_prod(prod_summ,cmpy_summ):
#   return AI_Analysis(prod_summ,cmpy_summ)

# # Streamlit Interface

# def main():
#     st.title("Product and Competitor Analysis Tool")

#     # Input company name
#     company_name = st.text_input("Enter Company Name", value="Sample Company")
#     email = st.text_input("Enter your email (Optional)")

#     # Perform Analysis
#     if st.button("Analyze"):
#         start_time = time.time()
        
#         st.write(f"Analyzing {company_name}...")
        
#         # Analyze product
#         prod_info = know_prod(company_name)
#         st.write("### Product Information:")
#         st.write(prod_info)

#         end_prd_anal = time.time()
#         st.write(f"Time taken to analyze the product: {(end_prd_anal - start_time)/60} Mins")

#         # Analyze competitors
#         st.write("### Competitor Information:")
#         otp = compi_main(company_name)
#         print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
#         print(otp)
#         st.write(otp)
#         end_compi = time.time()
#         st.write(f"Time taken to analyze the competitors: {end_compi - end_prd_anal} seconds")

#         # Summary
#         st.write("### Summary:")
#         cmpy_summ = summary_name(otp)
#         st.write(cmpy_summ)

#         end_time_summary = time.time()
#         st.write(f"Time taken to generate summary: {end_time_summary - end_compi} seconds")

#         analysis_total = analysis_prod(prod_info,cmpy_summ)
#         st.write("### Analysis:" + analysis_total)
#         # Send via email (Optional)
#         if email:
#             send_email_gmail(email, analysis_total)
#             st.write(f"Sending analysis to {email}...")
#             # (Implement email sending logic here)
#             # ...

# if __name__ == "__main__":
#     main()