basic.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from taipy import Gui
  2. import taipy as tp
  3. from taipy import Config, Core, Gui
  4. import taipy as tp
  5. import google.generativeai as palm
  6. import json
  7. from bs4 import BeautifulSoup
  8. palm.configure(api_key='<YOUR_PALM_API_KEY>')
  9. models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
  10. model = models[0].name
  11. print(model)
  12. # import random
  13. # import openai
  14. # import requests
  15. # from bs4 import BeautifulSoup
  16. # from metaphor_python import Metaphor
  17. # # Set your OpenAI API key
  18. # openai.api_key = "<YOUR_OPENAI_API_KEY>"
  19. # # Set your Metaphor API key
  20. # metaphor_api_key = "<YOUR_METAPHOR_API_KEY>"
  21. # metaphor = Metaphor(metaphor_api_key)
  22. import requests
  23. def scrape_article_content(url):
  24. try:
  25. # Send a GET request to the article URL
  26. response = requests.get(url)
  27. response.raise_for_status() # Raise an HTTPError for bad responses
  28. # Parse the HTML content of the page
  29. soup = BeautifulSoup(response.text, 'html.parser')
  30. # Extract the text content from the article
  31. article_text = ""
  32. for paragraph in soup.find_all('p'):
  33. article_text += paragraph.get_text() + '\n'
  34. return article_text
  35. except requests.exceptions.RequestException as e:
  36. print(f"Error: {e}")
  37. return None
  38. # # Example usage
  39. # article_url = "https://www.theatlantic.com/ideas/archive/2024/01/dance-salsa-relationships/677132/?utm_source=pocket-newtab-en-intl"
  40. # article_content = scrape_article_content(article_url)
  41. # if article_content:
  42. # print("Article Content:")
  43. # print(article_content)
  44. # # Set your input text
  45. # prompt = "Carefully read the following paragraph and create concise, informative notes highlighting the key points with sub headings and concepts. Structure the notes logically and clearly, making them easy to understand and review later"
  46. # prompt += article_content
  47. # completion = palm.generate_text(
  48. # model=model,
  49. # prompt=prompt,
  50. # temperature=0,
  51. # # The maximum length of the response
  52. # max_output_tokens=1000,
  53. # )
  54. # print(completion.result)
  55. #notes_text = completion.result
  56. # notes_json = {
  57. # "notes": notes_text.splitlines() # Split the text into a list of lines
  58. # }
  59. # # Convert the data structure to JSON format
  60. # notes_json_string = json.dumps(notes_json)
  61. # print(notes_json_string)
  62. input_url="input website url "
  63. qsn_no=1
  64. message=" "
  65. def submit_scenario(state):
  66. global input_url, qsn_no, message # Add this line to indicate that you're using the global variables
  67. input_url = state.input_url
  68. article_url = input_url
  69. article_content = scrape_article_content(article_url)
  70. print("article content bsdk" + article_content)
  71. prompt = "Carefully read the following paragraph and create concise, informative notes highlighting the key points with sub headings and concepts. Structure the notes logically and clearly, making them easy to understand and review later"
  72. prompt += article_content
  73. completion = palm.generate_text(
  74. model=model,
  75. prompt=prompt,
  76. temperature=0,
  77. # The maximum length of the response
  78. max_output_tokens=1000,
  79. )
  80. print(completion.result)
  81. #notes_text = completion.result
  82. message = completion.result
  83. state.message = message
  84. print(input_url)
  85. print("message "+message)
  86. # def format_notes(notes_text):
  87. # # Your logic to format the notes goes here
  88. # # This is a basic example, you may need to adapt it based on your specific requirements
  89. # sections = notes_text.split("**")
  90. # formatted_notes = ""
  91. # for i in range(1, len(sections), 2):
  92. # heading = sections[i].strip()
  93. # content = sections[i + 1].strip()
  94. # formatted_notes += f"\n\n{heading}\n{content}"
  95. # return formatted_notes
  96. page = """
  97. <style>
  98. </style>
  99. <|text-center|
  100. <h1>Echo Learn</h1>
  101. <p>A platform to give summary of any website </p>
  102. Url of website : <|{input_url}|input|> <|submit|button|on_action=submit_scenario|>
  103. >
  104. Ai Ans: <|{message}|text|>
  105. """
  106. # find_similar_urls_and_generate_quiz(input_url,qsn_no )
  107. if __name__ =="__main__":
  108. tp.Core().run()
  109. app=Gui(page)
  110. app.run(use_reloader=True)