from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import google.generativeai as genai
import os
import requests
from bs4 import BeautifulSoup
import validators
from youtube_transcript_api import YouTubeTranscriptApi
from cachetools import TTLCache
import asyncio
import hashlib
import time
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
app = FastAPI()
origins=[
"http://localhost:3000",
"chrome-extension://mjdggpghaabiocpngdkageeenpmgebim"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*']
)
model=genai.GenerativeModel("gemini-pro")
prompt1="""
As a Web Insight Summarizer, your task is to summarize the given web
content into a clear and informative text of around 250 words.
Highlight key points using bullet points or numbered lists to emphasize important details.
Keep the summary objective, coherent, and focused on the main insights without adding personal opinions.
Here's an example:
Main Topic: Climate Change
-Rising Temperatures: Due to greenhouse gas emissions.
-Consequences: Extreme weather events, rising sea levels, loss of biodiversity.
-Mitigation Strategies: Renewable energy, carbon reduction, sustainable practices.
-Call to Action: Urgent collective action needed.
Your summary should provide a comprehensive overview of the content, enabling readers to grasp the main insights efficiently.
Remember to keep the summary objective and focused on the key points of the content without adding unnecessary details or opinions.
Providing a clear and digestible overview will help users grasp the main insights from the parsed HTML content efficiently which is given in the following content :
"""
prompt2="""
As an Insight Summarizer, your task is to distill provided information into a concise and informative
summary of around 250 words. Utilize bullet points or numbered lists to highlight key
points and ensure clarity and coherence in your summary. Avoid injecting personal
opinions and focus solely on presenting the main insights from the content.
Here's an example:
Main Topic: [Main topic of the content]
[Key point 1]: [Description or detail]
[Key point 2]: [Description or detail]
[Key point 3]: [Description or detail]
Additional Considerations:
[Additional consideration 1]: [Description or detail]
[Additional consideration 2]: [Description or detail]
[Additional consideration 3]: [Description or detail]
Your summary should provide a comprehensive overview of the content, enabling readers to understand the main
insights efficiently. Remember to maintain objectivity and focus on the key points to ensure
clarity and usefulness for the reader
"""
prompt6="""
As an Insight Summarizer, your task is to distill provided information into a concise and informative
summary of around 250 words. Utilize bullet points or numbered lists to highlight key
points and ensure clarity and coherence in your summary. Avoid injecting personal
opinions and focus solely on presenting the main insights from the content.
Ensure to structure the content using HTML tags like
for paragraphs and
Main Topic:[Main topic of the content]
Additional Considerations:
Main Topic: Gardening Basics - A Beginner's Guide
Gardening is a fulfilling hobby that connects you with nature and allows you to create beautiful outdoor spaces. In this guide, Emma will walk you through the basics of gardening, helping you get started on your gardening journey.
Gardening involves cultivating and nurturing plants, flowers, and vegetables in outdoor spaces such as gardens, balconies, or even windowsills.
Here are some key points to remember:
As you gain experience and confidence in gardening, you can explore more advanced techniques and experiment with different plants and garden designs.
Gardening is not only a rewarding hobby but also a way to connect with the earth and cultivate a sense of wellbeing and stewardship for the environment.
""" prompt7=""" As an Insight Summarizer, your task is to distill provided information into a concise and informative summary of around 250 words. Utilize bullet points or numbered lists to highlight key points and ensure clarity and coherence in your summary. Avoid injecting personal opinions and focus solely on presenting the main insights from the content. Ensure to structure the content using HTML tags likefor paragraphs and
Main Topic:[Main topic of the content]
Additional Considerations:
Main Topic: The Impact of Artificial Intelligence on Business
for paragraphs and
end with
tag Example:Main Topic:The Impact of Artificial Intelligence on Business
for paragraphs and
Main Topic: Creating a Website from Scratch with HTML and CSS
Step 1: Planning Your Website
Before diving into coding, take some time to plan your website's structure, layout, and content. Consider the purpose of your website, target audience, and desired features. Sketch out wireframes or create mockups to visualize the design and user experience.
Step 2: Setting Up Your Development Environment
Ensure you have a text editor installed, such as Visual Studio Code or Sublime Text, for writing HTML and CSS code. Create a new project folder and organize your files neatly. Optionally, consider using version control systems like Git to track changes and collaborate with others.
Step 3: Creating the HTML Structure
Start by creating the basic structure of your website using HTML
Step 4: Adding Content and Text
Populate your website with relevant content, including headings, paragraphs, images, and links. Use descriptive text to communicate your message effectively and engage users. Organize content logically to improve readability and navigation.
Step 5: Styling with CSS
Apply styles to your website using CSS to enhance its appearance and layout. Create a separate CSS file and link it to your HTML document using thetag. Use selectors, properties, and values to customize fonts, colors, margins, padding, and more.
Step 6: Implementing Responsive Design
Ensure your website looks great and functions well across various devices and screen sizes. Use media queries and flexible layout techniques to create a responsive design that adapts to desktops, laptops, tablets, and smartphones seamlessly.
Step 7: Adding Navigation Menus
Implement navigation menus to help users navigate your website easily. Create a header or sidebar menu using HTML lists , and style it with CSS to enhance its appearance. Consider adding hover effects and transitions for better user interaction.
Step 8: Incorporating Multimedia Elements
Enhance your website with multimedia elements such as images, videos, and audio files. Use the image video tags to embed media content directly into your web pages. Optimize multimedia files for faster loading times and better performance.
Step 9: Implementing Forms and Interactivity
Add interactive elements to your website, such as forms, buttons, and animations, to engage users and collect feedback. Use HTML form elements like ,
Step 10: Testing and Debugging
Thoroughly test your website across different browsers and devices to ensure compatibility and functionality. Use developer tools to inspect and debug your code, fix any errors or inconsistencies, and optimize performance. Solicit feedback from peers or beta testers to identify areas for improvement.
Step 11: Deploying Your Website
Once you're satisfied with your website's design and functionality, it's time to deploy it to a web server. Choose a reliable hosting provider and upload your files using FTP or file manager tools. Configure domain settings and ensure proper security measures are in place.
""" content="" transcript="" def scrweb(url): global content if not validators.url(url): status=400 response="enter correct url" return status,response else: web=requests.get(url) status=web.status_code if status==200: soup=BeautifulSoup(web.content,"html.parser") paragraphs = soup.find_all(["p", "h1", "h2"]) text_list = [p.get_text() for p in paragraphs] # Combine text from all paragraphs into a single string content = ''.join(text_list) return status,content else: return {"unable to retrieve "} def genetate_gemini_content(prompt,content): response=model.generate_content([prompt,content]) return response.text def extract_transcript(youtube_link): try: global transcript if not validators.url(youtube_link): status=400 response="enter correct url" return status,response else: video_id=youtube_link.split("=")[1].split("&")[0] transcript_text=YouTubeTranscriptApi.get_transcript(video_id,languages = [ 'en']) for i in transcript_text: transcript+=" "+i["text"] return transcript except Exception as e: return "no information avaliable" # cache = TTLCache(maxsize=100, ttl=400) # @app.get("/summary") # async def test(url:str): # try: # cache_key = hashlib.sha256(url.encode()).hexdigest() # cached_response = cache.get(cache_key) # if cached_response: # print("hii") # return cached_response # status,content=scrweb(url) # if status==200: # response=genetate_gemini_content(prompt6,content) # # print(response) # cache[cache_key] = response # return response # else: # return status # except: # raise HTTPException(status_code=500, detail="Internal Server Error") cache={} # @app.on_event("startup") # async def startup_event(): # # Start a background task to clean up expired entries from the cache # asyncio.create_task(clean_up_cache()) # async def clean_up_cache(): # while True: # await asyncio.sleep(1000) # Check cache every 60 seconds # current_time = time.time() # expired_keys = [key for key, value in cache.items() if current_time - value["timestamp"] > 300] # for key in expired_keys: # del cache[key] @app.get("/summary") async def test(url:str): try: cache_key = hashlib.sha256(url.encode()).hexdigest() print(cache_key) # Check if the response is already cached if cache_key in cache: print("hi") return cache[cache_key] status,content=scrweb(url) if status==200: response=genetate_gemini_content(prompt6,content) print(response) cache[cache_key]=response return response else: return status except: raise HTTPException(status_code=500, detail="Internal Server Error") @app.get("/ut") async def test(): global transcript # Check if the input is a valid URL # if not validators.url(url): # raise HTTPException(status_code=400, detail="Invalid URL") return transcript @app.get("/wb") async def test(): global content # Check if the input is a valid URL # if not validators.url(url): # raise HTTPException(status_code=400, detail="Invalid URL") return content @app.post("/webqa") async def webchat(question:str): global content try: if content: response=model.generate_content([prompt4,content,question]) print(response.text) return response.text else: return {"please summarize first"} except: raise HTTPException(status_code=500, detail="Internal Server Error") @app.get("/youtube") async def ytranscript(url : str): global transcript transcript_text=extract_transcript(url) print(transcript_text) if transcript_text: try: print("hi") response=model.generate_content([prompt6,transcript_text]) print(response.text) return response.text except: raise HTTPException(status_code=500, detail="Internal Server Error") @app.post("/ytqanda") async def ytchat(question:str): global transcript try: if transcript: response=model.generate_content([prompt4,transcript,question]) print(response.text) return response.text else: return {"please summarize first"} except: raise HTTPException(status_code=500, detail="Internal Server Error") @app.get("/") async def test(): return {"status":"api is working"} @app.get("/rm") async def rm(): global transcript global content transcript="" content="" print("removed") return