File size: 6,383 Bytes
ecf1928 54a9a12 cbd471f ecf1928 cbd471f ecf1928 78d057d ecf1928 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
#!/usr/bin/env python
# coding: utf-8
import pandas as pd
from openai import OpenAI
import json
from tenacity import retry, wait_random_exponential, stop_after_attempt
@retry(wait=wait_random_exponential(min=1, max=3), stop=stop_after_attempt(2))
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 topic of the protest a local victim of police brutality?
Is the topic of the protest solidarity with non-local victims of police brutality?
Is the topic of the protest police brutality ?
Is the topic of the protest a policy reform related to reducing police brutality?
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()
|