blmtheme / app.py
NealCaren's picture
Update app.py
54a9a12
raw
history blame
6.12 kB
#!/usr/bin/env python
# coding: utf-8
# In[85]:
import pandas as pd
from openai import OpenAI
import json
def process_article(text, local_input, solidarity_input,issue_input,policy_input):
categories = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Protest Information",
"description": "A schema for categorizing newspaper articles about police brutality protests.",
"type": "object",
"name" : "ProtestCat",
"parameters": {
"type": "object",
"properties": {
"Local": {
"type": "boolean",
"description": f"{local_input}"
},
"Solidarity": {
"type": "boolean",
"description": f"{solidarity_input}"
},
"Issue": {
"type": "boolean",
"description": f"{issue_input}"
},
"Policy": {
"type": "boolean",
"description": f"{policy_input}"
},
"summary": {
"type": "string",
"description": "A focused summary of the article categorizing the protest details."
}
}
},
"required": [
"Local",
"Solidarity",
"Issue",
"Policy",
"summary"
]
}
messages = [
{"role": "system", "content": "You are a helpful assistant that extracts summaries of newspaper articles about political protests as JSON for a database. "},
{"role": "user", "content": f'''Extract information about the reason by a protest against police brutality from the following article.
Only use information from the article.
Answers the following questions:
Is the theme of the protest local?
Is the theme of the protest solidarity?
Is the theme of the protest issue?
Is the theme of the protest policy?
What best describes the theme?
ARTICLE: + {text}
''' }
]
client = OpenAI(api_key='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')
completion = client.chat.completions.create(
model='gpt-3.5-turbo-1106',
#model = 'gpt-4-1106-preview',
functions=[categories],
n=3,
messages=messages)
return pd.DataFrame([json.loads(c.message.function_call.arguments) for c in completion.choices])
# In[ ]:
# In[86]:
s = '''Another protest in support of the Black Lives Matter movement will take place Saturday at noon in Saugus Center.\n\nOrganized by Saugus Selectman Anthony Cogliano and resident Danielle Jones, the event comes two days after a group of local youths held their own demonstration — controversial for some of its participants’ views regarding police — in front of Saugus Town Hall.\n\n“It’s extremely important that we recognize the racial tensions and social injustices that have been happening around the world and right here in the town of Saugus,” Jones said of her reason for hosting the rally. “It’s clear many residents don’t understand that people of color experience discrimination on a daily basis. You’ll hear the masses say that’s not true, but that’s due to lack of knowledge or simply choosing to turn a blind eye.”\n\nCogliano and Jones originally planned to host a “unity rally” in collaboration with the young organizers of Thursday’s protest,'''
# In[87]:
li = '''Indicates if the protest is related to a local victim of police brutality. The protest is a response to an incident involving police brutality that occurred within the local community. The victim(s) mentioned are from the local area, and the protest is directly connected to this local context.'''
si = '''Indicates if the protest is in solidarity with a non-local victim of police brutality. The protest is organized to show solidarity with victims of police brutality from outside the local community. Even though the incident did not occur locally, the article covers protests occurring in the area that are supporting non-local victims. The focus is on the connection and support for individuals or events that are not part of the immediate locality.'''
ii = '''Indicates if the protest is about the issue of police brutality in general, without reference to specific victims. These articles discuss the general issue of police brutality without referring to specific victims. The protests are aimed at addressing the broader systemic problem rather than reacting to a particular incident.'''
pi = '''Indicates if the protest mentions a specific policy demand, such as banning chokeholds. The protest is centered around specific policy changes or legislative demands, such as banning chokeholds. Articles should contain explicit references to policy demands or proposals that the protesters are advocating for.'''
# In[88]:
import gradio as gr
# Assuming the necessary functions are in blm_coding_2023.py
# from blm_coding_2023 import your_processing_function
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
local_input = gr.Textbox(label="Local Definition", value=li, lines=3)
solidarity_input = gr.Textbox(label="Solidarity Definition", value=si, lines=3)
issue_input = gr.Textbox(label="Issue Definition", value=ii, lines=3)
policy_input = gr.Textbox(label="Policy Definition", value=pi, lines=3)
with gr.Column():
article_input = gr.Textbox(label="Test Newspaper Article", value = s, placeholder="Enter the text of the newspaper article here", lines=10)
submit_button = gr.Button("Submit")
with gr.Row():
output = gr.Dataframe() # This will display the output as a DataFrame
submit_button.click(process_article, inputs=[article_input,
local_input,
solidarity_input,
issue_input,
policy_input], outputs=output)
demo.launch()