# -*- coding: utf-8 -*- """app.ipynb Automatically generated by Colab. Original file is located at https://colab.research.google.com/drive/1vaVKGg6ycx5H06dSgRVN0d5CzcJquou5 """ import os from langchain.chains import LLMChain from langchain_core.prompts import PromptTemplate from langchain_groq import ChatGroq from googleapiclient.discovery import build import gradio as gr import html # For sanitizing user input # Load API keys securely from Hugging Face Secrets GROQ_API_KEY = os.getenv("GROQ_API_KEY") YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY") if not GROQ_API_KEY or not YOUTUBE_API_KEY: raise ValueError("❌ API keys are missing! Please add them in Hugging Face Secrets.") # Initialize the Groq model llm = ChatGroq(model="llama-3.3-70b-versatile", api_key=GROQ_API_KEY) # Updated Prompt Template prompt_template = PromptTemplate( input_variables=["user_input", "age_range"], template=( "**{user_input}**\n\n" "The user's selected age range is: **{age_range}**.\n\n" "Use Chain-of-Thought (CoT) reasoning to analyze the situation in context with the age range, then provide " "clear, actionable safety advice structured as follows:\n\n" "**### 🛡️Safety Tips🛡️**\n" "- Offer **practical** and **actionable** steps to stay safe, tailored to the selected age range.\n" "- Ensure the advice is **easy to follow** and **relevant** to the situation described.\n\n" "**### 🛡️Self-Defense Techniques🛡️**\n" "- Provide simple, **age-appropriate self-defense methods** that are easy to implement.\n" "- Focus on **safe** and **effective** techniques that suit the user's age and physical abilities.\n\n" "**### 🛡️Resources🛡️**\n" "- Recommend **relevant YouTube videos** for learning and practical demonstrations of safety measures." ) ) # Create the LLMChain chain = LLMChain(llm=llm, prompt=prompt_template) # Function to fetch YouTube videos with clickable links def search_youtube_videos(query, max_results=2): try: youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY) request = youtube.search().list( part="snippet", q=query, type="video", maxResults=max_results ) response = request.execute() video_html = "

🎥 Recommended Videos: