import gradio as gr from openai import OpenAI import os import tempfile import re # Initialize OpenAI client client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) def chicagize(input_text): """ Process the input text and return Chicago-style references. """ response = client.chat.completions.create( model="gpt-4o", messages=[ { "role": "system", "content": ''' Guidelines When givien a reference or list of references, return a list of correctly formatted references in Markdown. * Format each reference using Chicago Author-Year style (see samples below) * Do not use extra information about the articles. Rely only on the details provided. * Scan each reference for missing information, such as the author's first name or publication year. **If any information is missing, DO NOT invent or guess**. Instead, indicate the missing element using curly brackets, such as {YEAR} or {FIRST NAME}. * Present the references as a bullet list for clarity. Before listing the references, provide a list of: * Changes made to the original references. Detail each specific change. * Information that remains missing **Chicago Author-Date Style: Reference SAMPLES** **1. Book** * Della Porta, Donatella. 2015. *Mobilizing for Democracy: Comparing 1989 and 2011*. Oxford University Press. * Tarrow, Sidney G. 2011. *Power in Movement: Social Movements and Contentious Politics*. Cambridge University Press. **2. Chapter in an Edited Book** * Milkman, Ruth, Stephanie Luce, and Penny Lewis. 2013. "Globalizing Social Movements." In *The Wiley-Blackwell Companion to Globalization*, edited by George Ritzer, 584–605. Wiley-Blackwell. **3. Translated Book** * Touraine, Alain. 1981. *The Voice and the Eye: An Analysis of Social Movements*. Translated by Alan Duff. Cambridge University Press. **4. Book Consulted in an Electronic Format** * Castells, Manuel. 2012. *Networks of Outrage and Hope: Social Movements in the Internet Age*. Polity Press. ProQuest Ebook Central. **5. Journal Article** * Caren, Neal, and Sarah Gaby. 2014. "Occupy and Prefigurative Politics: The Limits and Possibilities of Creating a New World in the Shell of the Old." *Journal of Contemporary Ethnography* 43 (6): 668–96. [invalid URL removed]. * Earl, Jennifer, Andrew Martin, John D. McCarthy, and Sarah A. Soule. 2004. "The Use of Newspaper Data in the Study of Collective Action." *Annual Review of Sociology* 30:65–80. [invalid URL removed]. **6. News or Magazine Article** * Gitlin, Todd. 2011. "Occupy's Predicament: The Limits of the Politics of Space." *The Nation*, December 12. [invalid URL removed]. **7. Book Review** * Jasper, James M. 2012. "Review of *The Art of Moral Protest: Culture, Biography, and Creativity in Social Movements*, by James M. Jasper." *Contemporary Sociology* 41 (5): 636–38. **8. Interview** * Davis, Angela Y. 2016. "Angela Davis on Black Lives Matter, Feminism, and the Prison-Industrial Complex." Interview by Laura Flanders. *The Laura Flanders Show*, June 21. [URL]. **9. Thesis or Dissertation** * Corrigall-Brown, Catherine. 2016. "Contesting Citizenship: The Politics of Protest and the Struggle for Recognition in the South African AIDS Movement." PhD diss., University of California, Berkeley. **10. Web Page** * Movement for Black Lives. 2024. "About Us." Accessed January 5, 2024. [https://m4bl.org/about/](https://m4bl.org/about/). **11. Social Media Content** * Black Lives Matter (@Blklivesmatter). 2023. "Today we honor the legacy of Dr. Martin Luther King Jr. by continuing the fight for justice and equality for all. #MLKDay #BlackLivesMatter." Twitter, January 16. [URL]. **12. Video or Podcast** * Klein, Naomi. 2019. "Naomi Klein on the Climate Crisis: 'This Changes Everything'." *Democracy Now!*, September 19. Video, 54:17. [invalid URL removed]. **13. Personal Communication** * Not included in the reference list. Cite in the text only. This list provides examples of Chicago Author-Date reference list entries specifically focused on the topic of social movements and protest. Remember to consult the *Chicago Manual of Style* (17th edition) for comprehensive guidelines and more specific examples. ''' }, { "role": "user", "content": input_text } ], temperature=0.3, max_tokens=4096, ) return response.choices[0].message.content def chicagize_and_download(input_text): """ Process the input text and return Chicago-style references along with a download link. """ chicago_refs = chicagize(input_text) # Ensure each line starts with '- ' for Markdown bullet points chicago_refs_markdown = re.sub(r'^(?!- )', '- ', chicago_refs, flags=re.MULTILINE) # Remove blank lines chicago_refs_no_blanks = '\n'.join(line for line in chicago_refs_markdown.split('\n') if line.strip()) with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".md") as temp_file: temp_file.write(chicago_refs_no_blanks) return chicago_refs_markdown, temp_file.name # Set up the Gradio interface iface = gr.Interface( fn=chicagize_and_download, inputs=gr.Textbox(lines=10, label="Enter your references here"), outputs=[ gr.Markdown(label="Chicago-style references"), gr.File(label="Download Chicago-style references") ], title="Chicagizer: Convert to Chicago Author-Year Style", description="Enter your references and get them formatted in Chicago Author-Year style.", theme=gr.themes.Soft(), css=""" .gradio-container { font-family: 'Arial', sans-serif; } .gr-button { background-color: #4CAF50 !important; border: none !important; } .gr-button:hover { background-color: #45a049 !important; } """ ) # Launch the app iface.launch()