Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,14 +8,10 @@ Original file is located at
|
|
| 8 |
"""
|
| 9 |
|
| 10 |
import os
|
| 11 |
-
|
| 12 |
-
import html
|
| 13 |
from googleapiclient.discovery import build
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
from langchain.schema.runnable import RunnablePassthrough
|
| 17 |
-
from langchain_core.prompts import PromptTemplate
|
| 18 |
-
from langchain_groq import ChatGroq
|
| 19 |
|
| 20 |
# Load API keys securely from Hugging Face Secrets
|
| 21 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
@@ -35,23 +31,19 @@ prompt_template = PromptTemplate(
|
|
| 35 |
"The user's selected age range is: **{age_range}**.\n\n"
|
| 36 |
"Use Chain-of-Thought (CoT) reasoning to analyze the situation in context with the age range, then provide "
|
| 37 |
"clear, actionable safety advice structured as follows:\n\n"
|
| 38 |
-
"**### 1.
|
| 39 |
"- Offer **practical** and **actionable** steps to stay safe, tailored to the selected age range.\n"
|
| 40 |
"- Ensure the advice is **easy to follow** and **relevant** to the situation described.\n\n"
|
| 41 |
-
"**### 2.
|
| 42 |
"- Provide simple, **age-appropriate self-defense methods** that are easy to implement.\n"
|
| 43 |
"- Focus on **safe** and **effective** techniques that suit the user's age and physical abilities.\n\n"
|
| 44 |
-
"**### 3.
|
| 45 |
"- Recommend **relevant YouTube videos** for learning and practical demonstrations of safety measures."
|
| 46 |
)
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# Create
|
| 50 |
-
chain = (
|
| 51 |
-
{"user_input": RunnablePassthrough(), "age_range": RunnablePassthrough()}
|
| 52 |
-
| prompt_template
|
| 53 |
-
| llm
|
| 54 |
-
)
|
| 55 |
|
| 56 |
# Function to fetch YouTube videos with clickable links
|
| 57 |
def search_youtube_videos(query, max_results=2):
|
|
@@ -62,7 +54,7 @@ def search_youtube_videos(query, max_results=2):
|
|
| 62 |
)
|
| 63 |
response = request.execute()
|
| 64 |
|
| 65 |
-
video_html = "
|
| 66 |
for item in response.get("items", []):
|
| 67 |
video_id = item["id"].get("videoId")
|
| 68 |
video_title = html.escape(item["snippet"].get("title", "Untitled"))
|
|
@@ -86,72 +78,19 @@ def get_youtube_links(age_range):
|
|
| 86 |
# Safety Advice Function
|
| 87 |
def safety_advice(user_input, age_range):
|
| 88 |
if not user_input.strip():
|
| 89 |
-
return "
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
response_obj = chain.invoke({"user_input": user_input, "age_range": age_range})
|
| 93 |
-
|
| 94 |
-
# Extract just the content from the response object
|
| 95 |
-
response_content = response_obj.content if hasattr(response_obj, 'content') else str(response_obj)
|
| 96 |
-
|
| 97 |
links_html = get_youtube_links(age_range)
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
combined_output = f"""
|
| 101 |
-
<div class='output-box'>
|
| 102 |
-
<div class='advice-section'>
|
| 103 |
-
{response_content}
|
| 104 |
-
</div>
|
| 105 |
-
<div class='video-section'>
|
| 106 |
-
{links_html}
|
| 107 |
-
</div>
|
| 108 |
-
</div>
|
| 109 |
-
"""
|
| 110 |
-
|
| 111 |
-
return combined_output
|
| 112 |
|
| 113 |
# Function to clear fields
|
| 114 |
def clear_fields():
|
| 115 |
-
return "", "30+ (Adult)", ""
|
| 116 |
-
|
| 117 |
-
# Custom CSS for the output box
|
| 118 |
-
custom_css = """
|
| 119 |
-
.output-box {
|
| 120 |
-
border: 2px solid #4a90e2;
|
| 121 |
-
border-radius: 10px;
|
| 122 |
-
padding: 20px;
|
| 123 |
-
margin: 10px 0;
|
| 124 |
-
background-color: #f8f9fa;
|
| 125 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
.warning-box {
|
| 129 |
-
border-color: #e74c3c;
|
| 130 |
-
background-color: #fdeaea;
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
.advice-section {
|
| 134 |
-
margin-bottom: 20px;
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
.video-section {
|
| 138 |
-
border-top: 1px solid #e0e0e0;
|
| 139 |
-
padding-top: 15px;
|
| 140 |
-
}
|
| 141 |
-
|
| 142 |
-
.video-section a {
|
| 143 |
-
color: #4a90e2;
|
| 144 |
-
text-decoration: none;
|
| 145 |
-
font-weight: 500;
|
| 146 |
-
}
|
| 147 |
-
|
| 148 |
-
.video-section a:hover {
|
| 149 |
-
text-decoration: underline;
|
| 150 |
-
}
|
| 151 |
-
"""
|
| 152 |
|
| 153 |
# Gradio Interface
|
| 154 |
-
with gr.Blocks(
|
| 155 |
gr.Markdown("# 🛡️ SheGuard: Personal Safety Advisor 🛡️\n\n💡 Enter a situation where you feel unsafe, and receive expert safety advice tailored to your age range.")
|
| 156 |
|
| 157 |
with gr.Row():
|
|
@@ -160,14 +99,15 @@ with gr.Blocks(css=custom_css) as app:
|
|
| 160 |
"0-6 (Toddler)", "7-13 (Child)", "15-30 (Teenager to Young Adult)", "30+ (Adult)"
|
| 161 |
], label="Select Your Age Range", value="30+ (Adult)")
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
with gr.Row():
|
| 167 |
generate_button = gr.Button("Get Safety Advice")
|
| 168 |
clear_button = gr.Button("Clear")
|
| 169 |
|
| 170 |
-
generate_button.click(safety_advice, inputs=[situation_input, age_range_input], outputs=[
|
| 171 |
-
clear_button.click(clear_fields, inputs=[], outputs=[situation_input, age_range_input,
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
app.launch(share=True)
|
|
|
|
| 8 |
"""
|
| 9 |
|
| 10 |
import os
|
| 11 |
+
from langchain import PromptTemplate, LLMChain
|
|
|
|
| 12 |
from googleapiclient.discovery import build
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import html # For sanitizing user input
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Load API keys securely from Hugging Face Secrets
|
| 17 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
|
| 31 |
"The user's selected age range is: **{age_range}**.\n\n"
|
| 32 |
"Use Chain-of-Thought (CoT) reasoning to analyze the situation in context with the age range, then provide "
|
| 33 |
"clear, actionable safety advice structured as follows:\n\n"
|
| 34 |
+
"**### 1. Safety Tips:**\n"
|
| 35 |
"- Offer **practical** and **actionable** steps to stay safe, tailored to the selected age range.\n"
|
| 36 |
"- Ensure the advice is **easy to follow** and **relevant** to the situation described.\n\n"
|
| 37 |
+
"**### 2. Self-Defense Techniques:**\n"
|
| 38 |
"- Provide simple, **age-appropriate self-defense methods** that are easy to implement.\n"
|
| 39 |
"- Focus on **safe** and **effective** techniques that suit the user's age and physical abilities.\n\n"
|
| 40 |
+
"**### 3. Resources:**\n"
|
| 41 |
"- Recommend **relevant YouTube videos** for learning and practical demonstrations of safety measures."
|
| 42 |
)
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Create the LLMChain
|
| 46 |
+
chain = LLMChain(llm=llm, prompt=prompt_template)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Function to fetch YouTube videos with clickable links
|
| 49 |
def search_youtube_videos(query, max_results=2):
|
|
|
|
| 54 |
)
|
| 55 |
response = request.execute()
|
| 56 |
|
| 57 |
+
video_html = ""
|
| 58 |
for item in response.get("items", []):
|
| 59 |
video_id = item["id"].get("videoId")
|
| 60 |
video_title = html.escape(item["snippet"].get("title", "Untitled"))
|
|
|
|
| 78 |
# Safety Advice Function
|
| 79 |
def safety_advice(user_input, age_range):
|
| 80 |
if not user_input.strip():
|
| 81 |
+
return "⚠️ Please enter a situation to receive advice.", "<p></p>" # Empty HTML for videos
|
| 82 |
+
|
| 83 |
+
response = chain.run(user_input=user_input, age_range=age_range)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
links_html = get_youtube_links(age_range)
|
| 85 |
+
|
| 86 |
+
return f"{response}", links_html # Markdown for text, HTML for videos
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Function to clear fields
|
| 89 |
def clear_fields():
|
| 90 |
+
return "", "30+ (Adult)", "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Gradio Interface
|
| 93 |
+
with gr.Blocks() as app:
|
| 94 |
gr.Markdown("# 🛡️ SheGuard: Personal Safety Advisor 🛡️\n\n💡 Enter a situation where you feel unsafe, and receive expert safety advice tailored to your age range.")
|
| 95 |
|
| 96 |
with gr.Row():
|
|
|
|
| 99 |
"0-6 (Toddler)", "7-13 (Child)", "15-30 (Teenager to Young Adult)", "30+ (Adult)"
|
| 100 |
], label="Select Your Age Range", value="30+ (Adult)")
|
| 101 |
|
| 102 |
+
advice_output = gr.Markdown()
|
| 103 |
+
video_output = gr.HTML() # Using HTML for active YouTube links
|
| 104 |
+
|
| 105 |
with gr.Row():
|
| 106 |
generate_button = gr.Button("Get Safety Advice")
|
| 107 |
clear_button = gr.Button("Clear")
|
| 108 |
|
| 109 |
+
generate_button.click(safety_advice, inputs=[situation_input, age_range_input], outputs=[advice_output, video_output])
|
| 110 |
+
clear_button.click(clear_fields, inputs=[], outputs=[situation_input, age_range_input, advice_output, video_output])
|
| 111 |
+
|
| 112 |
+
app.launch(share = True)
|
| 113 |
|
|
|