from openai import OpenAI import gradio import os client = OpenAI( # This is the default and can be omitted api_key=os.environ["OPENAI_API_KEY"], ) def chat_with_gpt(prompt): response = client.chat.completions.create( model="gpt-4o", messages=[ { "role": "system", "content": [ { "type": "text", "text": "Election Info Helper provides clear, unbiased information about what's on the user's ballot in a structured table format. Each ballot item includes columns for 'What's on the Ballot,' 'Candidate,' and 'Endorsement.' Endorsements are based on the user's stated values and are linked to relevant sources for further details. - **Role and Goal**: Provide detailed and structured ballot information and endorsements, aligning endorsements with the user's values. Present this information in a table format that is easy to read. - **Constraints**: Avoid predicting election results, expressing political opinions, or endorsing candidates without clear value alignment. - **Guidelines**: Present information in a table with columns for 'What's on the Ballot,' 'Candidate,' and 'Endorsement,' including links to relevant sources for endorsements and additional information. Ensure clarity and readability. - **Clarification**: Confirm or request clarification on user values if not explicitly stated, to align endorsements accordingly. - **Personalization**: Maintain a friendly, concise, and informative tone to ensure the information is easy to read and understand." } ] }, { "role": "user", "content": [ { "text": prompt, "type": "text" } ] }, { "role": "assistant", "content": [ { "text": "Sure, I can help with that. Here is a table with the information on your ballot, focusing on candidates who support gun safety regulation:\n\n| Office | Candidate(s) & Endorsements |\n|---------------------------------------------|---------------------------------------------------------------------------------------------|\n| U.S. House of Representatives, District 13 | Lindsay Cross advocates for stronger gun control measures |\n| Governor of Florida | Candidate A supports gun safety regulation |\n| Florida State Senate, District 19 | Candidate B has a history of supporting gun control legislation |\n| Florida House of Representatives, District 61 | Candidate C is endorsed by gun safety advocacy groups |\n| Hillsborough County Sheriff | Candidate D has pledged to enforce stricter gun control laws |\n| Hillsborough County Commission, District 1 | Candidate E supports community-based gun violence prevention programs |\n| Hillsborough County School Board, District 3| Candidate F advocates for safer schools through gun control measures |\n\nPlease note that the specific names of candidates (other than Lindsay Cross) are placeholders and should be replaced with the actual candidates running in 2024. You can find the most up-to-date information on candidates and their endorsements closer to the election date.", "type": "text" } ] } ], temperature=0.04, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices[0].message.content.strip() demo = gradio.Interface(fn=chat_with_gpt, inputs = "textbox", outputs = "markdown", title = "Your Voter Guide" ) demo. launch(share =True)