Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,25 @@ from google.generativeai.types import HarmBlockThreshold, HarmCategory
|
|
| 8 |
# Configure Google API Key and model
|
| 9 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 10 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Safety and instruction settings
|
| 14 |
safety_settings = {
|
|
@@ -17,24 +35,25 @@ safety_settings = {
|
|
| 17 |
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
| 18 |
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
| 19 |
}
|
| 20 |
-
system_instructions = [
|
| 21 |
-
"You are an advocate against gender-based violence.",
|
| 22 |
-
"Analyze the content for signs of gender discrimination and provide actionable advice."
|
| 23 |
-
]
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Function to analyze text
|
| 26 |
def analyze_text(text):
|
| 27 |
-
prompt = f"Analyze this text for any instances of gender-based discrimination and provide tips
|
| 28 |
-
|
| 29 |
-
temperature=0.5,
|
| 30 |
-
max_output_tokens=300,
|
| 31 |
-
stop_sequences=["\n"],
|
| 32 |
-
top_k=40,
|
| 33 |
-
top_p=0.9,
|
| 34 |
-
system_instructions=system_instructions,
|
| 35 |
-
safety_settings=safety_settings
|
| 36 |
-
)
|
| 37 |
-
response = model.generate_content([prompt], generation_config=generation_config)
|
| 38 |
return response.text if response else "No response generated."
|
| 39 |
|
| 40 |
# Function to analyze images
|
|
@@ -53,55 +72,19 @@ def analyze_image(image):
|
|
| 53 |
response = model.generate_content([prompt, image], generation_config=generation_config)
|
| 54 |
return response.text if response else "No response generated."
|
| 55 |
|
| 56 |
-
# Function to analyze videos
|
| 57 |
-
def analyze_video(video):
|
| 58 |
-
prompt = "Analyze this video for any instances of gender-based discrimination and provide tips."
|
| 59 |
-
clip = mp.VideoFileClip(video.name)
|
| 60 |
-
frame = clip.get_frame(1) # Get a frame at t=1 second
|
| 61 |
-
image = Image.fromarray(frame)
|
| 62 |
-
image = image.convert("RGB")
|
| 63 |
-
generation_config = genai.types.GenerationConfig(
|
| 64 |
-
temperature=0.5,
|
| 65 |
-
max_output_tokens=300,
|
| 66 |
-
stop_sequences=["\n"],
|
| 67 |
-
top_k=40,
|
| 68 |
-
top_p=0.9,
|
| 69 |
-
system_instructions=system_instructions,
|
| 70 |
-
safety_settings=safety_settings
|
| 71 |
-
)
|
| 72 |
-
response = model.generate_content([prompt, image], generation_config=generation_config)
|
| 73 |
-
return response.text if response else "No response generated."
|
| 74 |
|
| 75 |
# Gradio interface setup
|
| 76 |
with gr.Blocks() as app:
|
| 77 |
with gr.Tab("Text Analysis"):
|
| 78 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type here...", lines=4)
|
| 79 |
-
text_output = gr.Textbox(label="Analysis Output", lines=6)
|
| 80 |
analyze_text_btn = gr.Button("Analyze Text")
|
|
|
|
| 81 |
analyze_text_btn.click(
|
| 82 |
fn=analyze_text,
|
| 83 |
inputs=text_input,
|
| 84 |
outputs=text_output
|
| 85 |
)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
image_output = gr.Textbox(label="Analysis Output", lines=6)
|
| 90 |
-
analyze_image_btn = gr.Button("Analyze Image")
|
| 91 |
-
analyze_image_btn.click(
|
| 92 |
-
fn=analyze_image,
|
| 93 |
-
inputs=image_input,
|
| 94 |
-
outputs=image_output
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
with gr.Tab("Video Analysis"):
|
| 98 |
-
video_input = gr.Video(label="Upload Video")
|
| 99 |
-
video_output = gr.Textbox(label="Analysis Output", lines=6)
|
| 100 |
-
analyze_video_btn = gr.Button("Analyze Video")
|
| 101 |
-
analyze_video_btn.click(
|
| 102 |
-
fn=analyze_video,
|
| 103 |
-
inputs=video_input,
|
| 104 |
-
outputs=video_output
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
app.launch()
|
|
|
|
| 8 |
# Configure Google API Key and model
|
| 9 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 10 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 11 |
+
MODEL_ID = "gemini-1.5-pro-002"
|
| 12 |
+
model = GenerativeModel(MODEL_ID)
|
| 13 |
+
|
| 14 |
+
example_model = GenerativeModel(
|
| 15 |
+
MODEL_ID,
|
| 16 |
+
system_instruction=[
|
| 17 |
+
"You are an advocate against gender-based violence.",
|
| 18 |
+
"Analyze the content for signs of gender discrimination and provide actionable advice."
|
| 19 |
+
],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Set model parameters
|
| 23 |
+
generation_config = GenerationConfig(
|
| 24 |
+
temperature=0.9,
|
| 25 |
+
top_p=1.0,
|
| 26 |
+
top_k=32,
|
| 27 |
+
candidate_count=1,
|
| 28 |
+
max_output_tokens=8192,
|
| 29 |
+
)
|
| 30 |
|
| 31 |
# Safety and instruction settings
|
| 32 |
safety_settings = {
|
|
|
|
| 35 |
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
| 36 |
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
| 37 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
prompt = """
|
| 40 |
+
Analyze this text for any instances of gender-based discrimination and provide tips
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
# Set contents to send to the model
|
| 44 |
+
contents = [prompt]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# Prompt the model to generate content
|
| 48 |
+
response = example_model.generate_content(
|
| 49 |
+
contents,
|
| 50 |
+
generation_config=generation_config,
|
| 51 |
+
safety_settings=safety_settings,
|
| 52 |
+
)
|
| 53 |
# Function to analyze text
|
| 54 |
def analyze_text(text):
|
| 55 |
+
prompt = f"Analyze this text for any instances of gender-based discrimination and provide tips{text}"
|
| 56 |
+
response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
return response.text if response else "No response generated."
|
| 58 |
|
| 59 |
# Function to analyze images
|
|
|
|
| 72 |
response = model.generate_content([prompt, image], generation_config=generation_config)
|
| 73 |
return response.text if response else "No response generated."
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Gradio interface setup
|
| 77 |
with gr.Blocks() as app:
|
| 78 |
with gr.Tab("Text Analysis"):
|
| 79 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type here...", lines=4)
|
|
|
|
| 80 |
analyze_text_btn = gr.Button("Analyze Text")
|
| 81 |
+
text_output = gr.Textbox(label="Analysis Output", lines=6)
|
| 82 |
analyze_text_btn.click(
|
| 83 |
fn=analyze_text,
|
| 84 |
inputs=text_input,
|
| 85 |
outputs=text_output
|
| 86 |
)
|
| 87 |
|
| 88 |
+
|
| 89 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
app.launch()
|