bigideas / app.py
ambrosfitz's picture
Update app.py
d5c5fae verified
import random
import requests
import gradio as gr
import os
# Define the time periods and their corresponding key terms
time_periods = {
"1865-1910": ["Freedmen's Bureau", "Black Codes and Jim Crow", "The Fourteenth Amendment", "The Reconstruction Era", "The Fifteenth Amendment", "Manifest Destiny", "Transcontinental Railroad", "\"Buffalo Soldiers\"", "Robber Barons", "Andrew Carnegie", "Social Darwinism", "John D. Rockefeller", "J. Pierpont Morgan", "Cornelius Vanderbilt", "The Gilded Age", "Boss William Tweed", "Compromise of 1877", "yellow journalism", "Spanish American War", "Rough Riders", "Anti-Imperialist League", "muckrakers", "Progressivism", "Alice Paul", "The Anti-Suffragist Movement", "Booker T. Washington", "W.E.B. DuBois", "Niagara Movement", "National Association for the Advancement of Colored People (NAACP)", "Woodrow Wilson's Early Efforts at Foreign Policy"],
"1910-1960": ["Neutrality", "Lusitania", "Zimmermann telegram", "Women in WWI", "African Americans in WWI", "Prohibition", "Nativism", "Emergency Immigration Act of 1921", "Ku Klux Klan (KKK) and the 2nd Ku Klux Klan", "Scopes Monkey Trial", "The \"New Woman\" and Flappers", "The Harlem Renaissance", "Stock market crash/Black Tuesday", "Speculation in the 1920s", "Bank runs/bank closures", "Unemployment", "Herbert Hoover's Initial Reaction", "Public Reaction to Hoover", "Bonus Army", "Reforming the Banking Crisis/Emergency Banking Act", "Fireside Chats", "Relief: Employment for the Masses", "Rescuing Farms and Factories", "Regional Planning/Tennessee Valley Authority", "Huey Long", "Social Security Act", "Wagner Act a.k.a. the National Labor Relations Act", "Supreme Court Packing Plan", "Change in Federal Government's Scope (In the Final Analysis)", "Totalitarianism & fascism in Europe", "Japan militarism", "The Atlantic Charter", "Pearl Harbor, Hawaii attack, \"A Date Which Will Live in Infamy\"", "Women in the War: Rosie the Riveter and Beyond", "African Americans and Double V", "Zoot Suit Riots/Bracero Program", "Internment", "Executive Order 9066", "containment", "domino theory", "Truman Doctrine", "Marshall Plan", "\"Showdown in Europe\" (Berlin blockade)", "House Un-American Activities Committee", "blacklist", "\"To the Trenches Again\" (Korean War)", "military-industrial complex", "Sputnik", "Levittown", "baby boom", "Sweatt v. Painter", "Alice Coachman", "Brown v. Board of Education of Topeka", "Thurgood Marshall", "Little Rock Nine", "Montgomery Bus Boycott", "Southern Christian Leadership Conference (SCLC)", "Student Nonviolent Coordinating Committee (SNCC)", "Freedom Riders", "Letter from Birmingham Jail", "Malcolm X"],
"1960-present": ["The Great Society", "\"Bloody Sunday\"", "Gulf of Tonkin Resolution", "Tet Offensive", "counterculture", "\"My Lai\" (My Lai village massacre)", "Vietnamization", "Kent State shootings", "Pentagon Papers", "Dolores Huerta", "César Chavez", "National Farm Workers Association (NFWA)", "Chicano Movement", "Port Huron Statement", "Betty Friedan", "Title VII of the Civil Rights Act of 1964", "National Organization for Women (NOW)", "American Indian Movement (AIM)", "Shirley Chisholm", "Equal Rights Amendment (ERA)", "Richard Nixon", "silent majority", "Nixon's Domestic Policies (\"The Domestic Nixon\")", "détente", "Strategic Arms Limitation Treaty (SALT)", "\"plumbers\"/Committee to Re-Elect the President (CREEP)", "Watergate break-in scandal", "Gerald R. Ford", "Ronald Reagan", "The New Right", "Reaganomics", "The Heritage Foundation", "Moral Majority", "Phyllis Schlafly", "The War on Drugs", "George W. Bush", "9/11", "War in Afghanistan and Iraq", "USA Patriot Act", "Terrorist Surveillance Program", "Hamdan v. Rumsfeld", "Naval Base at Guantanamo Bay"]
}
# Define the Big Ideas
big_ideas = ["American Identity", "Labor and Technology", "America and the World", "Reform and Renewal", "Self and Society"]
def get_llm_response(prompt):
endpoint = 'https://api.together.xyz/v1/chat/completions'
headers = {
"Authorization": f"Bearer {os.environ['BEARER_TOKEN']}",
}
data = {
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"max_tokens": 2048,
"temperature": 0.7,
"top_p": 0.7,
"top_k": 50,
"repetition_penalty": 1,
"stop": [
"[/INST]",
"</s>"
],
"messages": [
{
"role": "system",
"content": "You are a helpful assistant that provides insightful explanations."
},
{
"role": "user",
"content": prompt
}
]
}
response = requests.post(endpoint, json=data, headers=headers)
if response.status_code == 200:
return response.json()['choices'][0]['message']['content']
else:
raise Exception(f"API request failed with status code {response.status_code}")
def describe_progression(big_idea, combination):
prompt = f"Explain how the key terms '{combination[0]}' (1865-1910), '{combination[1]}' (1910-1960), and '{combination[2]}' (1960-present) demonstrate the progression and connection of {big_idea} throughout U.S. History. Provide a detailed explanation for each key term, highlighting the historical context and significance, and how they collectively show the progression of the Big Idea across these time periods."
description = get_llm_response(prompt)
return description
def generate_response(big_idea, term1, term2, term3):
combination = [term1, term2, term3]
description = describe_progression(big_idea, combination)
return f"Selected combination for '{big_idea}': {combination}\n\n{description}"
demo = gr.Interface(
fn=generate_response,
inputs=[
gr.Dropdown(choices=big_ideas, label="Select a Big Idea"),
gr.Dropdown(choices=time_periods["1865-1910"], label="Select a key term from 1865-1910"),
gr.Dropdown(choices=time_periods["1910-1960"], label="Select a key term from 1910-1960"),
gr.Dropdown(choices=time_periods["1960-present"], label="Select a key term from 1960-present")
],
outputs="text",
title="U.S. History Big Ideas Progression",
description="Select a Big Idea and three key terms (one from each time period) to generate a description of their progression and connection throughout U.S. History.",
)
demo.launch()