Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,140 @@
|
|
1 |
import gradio as gr
|
2 |
import networkx as nx
|
3 |
import matplotlib.pyplot as plt
|
4 |
-
from transformers import pipeline
|
5 |
-
import spacy
|
6 |
-
import torch
|
7 |
from pathlib import Path
|
|
|
|
|
8 |
|
9 |
-
# Load
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
# Define categories and their colors
|
14 |
CATEGORIES = {
|
15 |
-
"Main Theme": "#
|
16 |
-
"Event": "#
|
17 |
-
"Person": "#
|
18 |
-
"Law": "#
|
19 |
-
"Concept": "#
|
20 |
}
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
def
|
31 |
-
"""
|
32 |
term_lower = term.lower()
|
33 |
-
text_lower = text.lower()
|
34 |
-
|
35 |
-
# Find the term in text
|
36 |
-
index = text_lower.find(term_lower)
|
37 |
-
if index == -1:
|
38 |
-
return ""
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
def categorize_term(term, doc):
|
47 |
-
"""Categorize a term based on NER and custom rules."""
|
48 |
for ent in doc.ents:
|
49 |
-
if
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
# Custom categorization for common terms
|
58 |
-
themes = ["manifest destiny", "reconstruction", "civil war", "slavery"]
|
59 |
-
if term.lower() in themes:
|
60 |
-
return "Main Theme"
|
61 |
-
|
62 |
return "Concept"
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
def generate_context_map(term):
|
65 |
"""Generate a network visualization for the given term."""
|
66 |
if not term or not term.strip():
|
67 |
return None
|
68 |
|
69 |
-
# Load content
|
70 |
-
content = load_content()
|
71 |
-
if not content:
|
72 |
-
return None
|
73 |
-
|
74 |
-
# Get context
|
75 |
-
context = find_context(term, content)
|
76 |
-
if not context:
|
77 |
-
return None
|
78 |
-
|
79 |
-
# Process context
|
80 |
-
doc = nlp(context)
|
81 |
-
|
82 |
# Create graph
|
83 |
G = nx.Graph()
|
84 |
|
85 |
# Add main term
|
86 |
-
|
87 |
-
|
|
|
88 |
|
89 |
-
#
|
90 |
-
|
91 |
-
for
|
92 |
-
if
|
93 |
-
|
94 |
-
|
95 |
-
'category': categorize_term(ent.text, doc)
|
96 |
-
})
|
97 |
-
|
98 |
-
# Add top related entities (limit to 8)
|
99 |
-
for entity in related_entities[:8]:
|
100 |
-
G.add_node(entity['text'], category=entity['category'])
|
101 |
-
G.add_edge(term, entity['text'])
|
102 |
|
103 |
# Create visualization
|
104 |
plt.figure(figsize=(12, 12))
|
105 |
plt.clf()
|
106 |
|
107 |
-
# Set
|
108 |
-
plt.gca().set_facecolor('#
|
109 |
-
plt.gcf().set_facecolor('#
|
110 |
|
111 |
# Create layout
|
112 |
-
pos = nx.spring_layout(G, k=1)
|
113 |
|
114 |
# Draw nodes for each category
|
115 |
for category, color in CATEGORIES.items():
|
@@ -119,38 +144,53 @@ def generate_context_map(term):
|
|
119 |
nx.draw_networkx_nodes(G, pos,
|
120 |
nodelist=node_list,
|
121 |
node_color=color,
|
122 |
-
node_size=
|
|
|
123 |
|
124 |
# Draw edges
|
125 |
-
nx.draw_networkx_edges(G, pos, edge_color='
|
126 |
|
127 |
-
# Add labels
|
128 |
-
nx.draw_networkx_labels(G, pos,
|
|
|
|
|
|
|
129 |
|
130 |
-
# Add title
|
131 |
-
plt.title(f"Historical Context Map for '{
|
132 |
-
|
133 |
pad=20)
|
134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
return plt.gcf()
|
136 |
|
137 |
# Create Gradio interface
|
138 |
iface = gr.Interface(
|
139 |
fn=generate_context_map,
|
140 |
inputs=gr.Textbox(
|
141 |
-
label="Enter a historical term from Unit 5",
|
142 |
placeholder="e.g., Civil War, Abraham Lincoln, Reconstruction"
|
143 |
),
|
144 |
outputs=gr.Plot(),
|
145 |
title="Historical Context Mapper",
|
146 |
-
description="Enter a term from Unit 5 (1844-1877) to see its historical context and connections.
|
|
|
147 |
examples=[
|
148 |
["Civil War"],
|
149 |
-
["Abraham Lincoln"],
|
150 |
["Reconstruction"],
|
151 |
["Manifest Destiny"],
|
152 |
-
["
|
153 |
-
|
|
|
|
|
154 |
)
|
155 |
|
156 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
import networkx as nx
|
3 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
4 |
from pathlib import Path
|
5 |
+
import spacy
|
6 |
+
import re
|
7 |
|
8 |
+
# Load spaCy model
|
9 |
+
try:
|
10 |
+
nlp = spacy.load("en_core_web_sm")
|
11 |
+
except OSError:
|
12 |
+
print("Downloading spaCy model...")
|
13 |
+
import subprocess
|
14 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
15 |
+
nlp = spacy.load("en_core_web_sm")
|
16 |
|
17 |
+
# Define categories and their colors with better contrast
|
18 |
CATEGORIES = {
|
19 |
+
"Main Theme": "#4287f5", # Bright blue
|
20 |
+
"Event": "#42f54b", # Bright green
|
21 |
+
"Person": "#f542aa", # Pink
|
22 |
+
"Law": "#f5d442", # Yellow
|
23 |
+
"Concept": "#f54242" # Red
|
24 |
}
|
25 |
|
26 |
+
# Sample historical data for Unit 5 (1844-1877)
|
27 |
+
HISTORICAL_DATA = {
|
28 |
+
"civil war": {
|
29 |
+
"category": "Main Theme",
|
30 |
+
"related": [
|
31 |
+
("Abraham Lincoln", "Person"),
|
32 |
+
("Emancipation Proclamation", "Law"),
|
33 |
+
("Confederate States", "Concept"),
|
34 |
+
("Union Army", "Concept"),
|
35 |
+
("Battle of Gettysburg", "Event"),
|
36 |
+
("Slavery", "Main Theme"),
|
37 |
+
("Robert E. Lee", "Person"),
|
38 |
+
("Ulysses S. Grant", "Person")
|
39 |
+
]
|
40 |
+
},
|
41 |
+
"reconstruction": {
|
42 |
+
"category": "Main Theme",
|
43 |
+
"related": [
|
44 |
+
("13th Amendment", "Law"),
|
45 |
+
("14th Amendment", "Law"),
|
46 |
+
("15th Amendment", "Law"),
|
47 |
+
("Freedmen's Bureau", "Concept"),
|
48 |
+
("Andrew Johnson", "Person"),
|
49 |
+
("Black Codes", "Law"),
|
50 |
+
("Radical Republicans", "Concept"),
|
51 |
+
("Carpetbaggers", "Concept")
|
52 |
+
]
|
53 |
+
},
|
54 |
+
"manifest destiny": {
|
55 |
+
"category": "Main Theme",
|
56 |
+
"related": [
|
57 |
+
("Mexican-American War", "Event"),
|
58 |
+
("Oregon Territory", "Concept"),
|
59 |
+
("California Gold Rush", "Event"),
|
60 |
+
("James K. Polk", "Person"),
|
61 |
+
("Treaty of Guadalupe Hidalgo", "Law"),
|
62 |
+
("Westward Expansion", "Concept"),
|
63 |
+
("Native American Displacement", "Event"),
|
64 |
+
("Mexican Cession", "Event")
|
65 |
+
]
|
66 |
+
}
|
67 |
+
}
|
68 |
|
69 |
+
def categorize_term(term):
|
70 |
+
"""Categorize a term based on predefined data and NER."""
|
71 |
term_lower = term.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
# Check predefined categories first
|
74 |
+
if term_lower in HISTORICAL_DATA:
|
75 |
+
return HISTORICAL_DATA[term_lower]["category"]
|
76 |
|
77 |
+
# Use spaCy for NER
|
78 |
+
doc = nlp(term)
|
|
|
|
|
79 |
for ent in doc.ents:
|
80 |
+
if ent.label_ == "PERSON":
|
81 |
+
return "Person"
|
82 |
+
elif ent.label_ == "EVENT" or ent.label_ == "DATE":
|
83 |
+
return "Event"
|
84 |
+
elif ent.label_ == "LAW" or ent.label_ == "ORG":
|
85 |
+
return "Law"
|
86 |
+
|
87 |
+
# Default to Concept if no other category is found
|
|
|
|
|
|
|
|
|
|
|
88 |
return "Concept"
|
89 |
|
90 |
+
def get_related_terms(term):
|
91 |
+
"""Get related terms for a given historical term."""
|
92 |
+
term_lower = term.lower()
|
93 |
+
|
94 |
+
if term_lower in HISTORICAL_DATA:
|
95 |
+
return HISTORICAL_DATA[term_lower]["related"]
|
96 |
+
|
97 |
+
# If term not in predefined data, return some general connections
|
98 |
+
# based on the time period
|
99 |
+
general_connections = [
|
100 |
+
("Civil War", "Main Theme"),
|
101 |
+
("Reconstruction", "Main Theme"),
|
102 |
+
("Abraham Lincoln", "Person"),
|
103 |
+
("Slavery", "Concept"),
|
104 |
+
("United States", "Concept")
|
105 |
+
]
|
106 |
+
return general_connections[:5] # Limit to 5 connections
|
107 |
+
|
108 |
def generate_context_map(term):
|
109 |
"""Generate a network visualization for the given term."""
|
110 |
if not term or not term.strip():
|
111 |
return None
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
# Create graph
|
114 |
G = nx.Graph()
|
115 |
|
116 |
# Add main term
|
117 |
+
main_term = term.strip()
|
118 |
+
term_category = categorize_term(main_term)
|
119 |
+
G.add_node(main_term, category=term_category)
|
120 |
|
121 |
+
# Add related terms
|
122 |
+
related_terms = get_related_terms(main_term)
|
123 |
+
for related_term, category in related_terms:
|
124 |
+
if related_term.lower() != main_term.lower():
|
125 |
+
G.add_node(related_term, category=category)
|
126 |
+
G.add_edge(main_term, related_term)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
# Create visualization
|
129 |
plt.figure(figsize=(12, 12))
|
130 |
plt.clf()
|
131 |
|
132 |
+
# Set light background for better contrast
|
133 |
+
plt.gca().set_facecolor('#ffffff')
|
134 |
+
plt.gcf().set_facecolor('#ffffff')
|
135 |
|
136 |
# Create layout
|
137 |
+
pos = nx.spring_layout(G, k=1.5, iterations=50)
|
138 |
|
139 |
# Draw nodes for each category
|
140 |
for category, color in CATEGORIES.items():
|
|
|
144 |
nx.draw_networkx_nodes(G, pos,
|
145 |
nodelist=node_list,
|
146 |
node_color=color,
|
147 |
+
node_size=3000,
|
148 |
+
alpha=0.7)
|
149 |
|
150 |
# Draw edges
|
151 |
+
nx.draw_networkx_edges(G, pos, edge_color='gray', width=2, alpha=0.6)
|
152 |
|
153 |
+
# Add labels with better formatting
|
154 |
+
labels = nx.draw_networkx_labels(G, pos,
|
155 |
+
font_size=10,
|
156 |
+
font_weight='bold',
|
157 |
+
font_color='black')
|
158 |
|
159 |
+
# Add title and legend
|
160 |
+
plt.title(f"Historical Context Map for '{main_term}'",
|
161 |
+
fontsize=16,
|
162 |
pad=20)
|
163 |
|
164 |
+
# Add category legend
|
165 |
+
legend_elements = [plt.Line2D([0], [0], marker='o', color='w',
|
166 |
+
markerfacecolor=color, markersize=10,
|
167 |
+
label=category)
|
168 |
+
for category, color in CATEGORIES.items()]
|
169 |
+
plt.legend(handles=legend_elements, loc='upper left',
|
170 |
+
bbox_to_anchor=(1, 1))
|
171 |
+
|
172 |
+
plt.tight_layout()
|
173 |
return plt.gcf()
|
174 |
|
175 |
# Create Gradio interface
|
176 |
iface = gr.Interface(
|
177 |
fn=generate_context_map,
|
178 |
inputs=gr.Textbox(
|
179 |
+
label="Enter a historical term from Unit 5 (1844-1877)",
|
180 |
placeholder="e.g., Civil War, Abraham Lincoln, Reconstruction"
|
181 |
),
|
182 |
outputs=gr.Plot(),
|
183 |
title="Historical Context Mapper",
|
184 |
+
description="""Enter a term from Unit 5 (1844-1877) to see its historical context and connections.
|
185 |
+
The visualization will show how the term relates to key events, people, laws, and concepts from this period.""",
|
186 |
examples=[
|
187 |
["Civil War"],
|
|
|
188 |
["Reconstruction"],
|
189 |
["Manifest Destiny"],
|
190 |
+
["Abraham Lincoln"],
|
191 |
+
["Emancipation Proclamation"]
|
192 |
+
],
|
193 |
+
theme="default"
|
194 |
)
|
195 |
|
196 |
if __name__ == "__main__":
|