Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -73,7 +73,7 @@ file_path = hf_hub_download(
|
|
| 73 |
|
| 74 |
with open(file_path, 'r') as f:
|
| 75 |
hadith_lookup_dict = json.load(f)
|
| 76 |
-
|
| 77 |
|
| 78 |
|
| 79 |
def value_to_hex(value):
|
|
@@ -90,6 +90,27 @@ def get_node_info(node):
|
|
| 90 |
node_name = info.get('Famous Name', 'فلان')
|
| 91 |
return info, student_narrations, student_gen, student_rank, node_name
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def visualize_isnad(taraf_num, yaxis):
|
| 95 |
# Precompute filtered dataframes
|
|
@@ -105,7 +126,7 @@ def visualize_isnad(taraf_num, yaxis):
|
|
| 105 |
|
| 106 |
for i, hadith_parts in enumerate(taraf_hadith):
|
| 107 |
# look up hadith for each bookid_hadithid
|
| 108 |
-
isnad_hadith1 = isnad_info.iloc[
|
| 109 |
|
| 110 |
# Create graph and find end nodes
|
| 111 |
G = nx.from_pandas_edgelist(isnad_hadith1, source='Source', target='Destination', create_using=nx.DiGraph())
|
|
@@ -220,11 +241,7 @@ def visualize_subTaraf(taraf_num, hadith_str, yaxis):
|
|
| 220 |
taraf['Index'] = np.arange(num_hadith)
|
| 221 |
sub_taraf = taraf[taraf['Index'].isin(hadiths)]
|
| 222 |
|
| 223 |
-
|
| 224 |
-
list_of_lists = [hadith_lookup[i] for i in taraf_hadith]
|
| 225 |
-
flattened = list(set([elem for sublist in list_of_lists for elem in sublist]))
|
| 226 |
-
|
| 227 |
-
isnad_hadith = isnad_info.iloc[flattened]
|
| 228 |
isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(int)
|
| 229 |
|
| 230 |
# Merge isnad_hadith with narrator_bios for Teacher and Student
|
|
@@ -269,10 +286,7 @@ def visualize_hadith_isnad(df, yaxis):
|
|
| 269 |
df['bookid_hadithid'] = df['Book_ID'].astype(str) + '_' + df['Hadith Number'].astype(str)
|
| 270 |
hadith = matn_info[matn_info['bookid_hadithid'].isin(df['bookid_hadithid'])]
|
| 271 |
taraf_hadith = df['bookid_hadithid'].to_list()
|
| 272 |
-
|
| 273 |
-
flattened = list(set([elem for sublist in list_of_lists for elem in sublist]))
|
| 274 |
-
|
| 275 |
-
isnad_hadith = isnad_info.iloc[flattened]
|
| 276 |
isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(int)
|
| 277 |
|
| 278 |
# Merge isnad_hadith with narrator_bios for Teacher and Student
|
|
@@ -330,7 +344,7 @@ def visualize_narrator_taraf(taraf_num, narrator, yaxis):
|
|
| 330 |
|
| 331 |
# Process each hadith in taraf_hadith_split
|
| 332 |
for idx, split_hadith in enumerate(taraf_hadith):
|
| 333 |
-
isnad_hadith1 = isnad_info.iloc[
|
| 334 |
G1 = nx.from_pandas_edgelist(isnad_hadith1, source='Source', target='Destination', create_using=nx.DiGraph())
|
| 335 |
if narrator in G1.nodes:
|
| 336 |
matns_with_narrator.append(taraf_hadith[idx])
|
|
|
|
| 73 |
|
| 74 |
with open(file_path, 'r') as f:
|
| 75 |
hadith_lookup_dict = json.load(f)
|
| 76 |
+
HADITH_LOOKUP = defaultdict(list, hadith_lookup_dict)
|
| 77 |
|
| 78 |
|
| 79 |
def value_to_hex(value):
|
|
|
|
| 90 |
node_name = info.get('Famous Name', 'فلان')
|
| 91 |
return info, student_narrations, student_gen, student_rank, node_name
|
| 92 |
|
| 93 |
+
def lookup_hadith(taraf_hadith, hadith_lookup):
|
| 94 |
+
"""
|
| 95 |
+
Returns a list of unique elements from the hadith_lookup for the given taraf_hadith.
|
| 96 |
+
|
| 97 |
+
Parameters:
|
| 98 |
+
taraf_hadith (str or list of str): A string or list of strings to look up.
|
| 99 |
+
hadith_lookup (defaultdict): A defaultdict containing the hadith data.
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
list: A list of unique elements from the lookup results.
|
| 103 |
+
"""
|
| 104 |
+
# Ensure taraf_hadith is always a list
|
| 105 |
+
if isinstance(taraf_hadith, str):
|
| 106 |
+
taraf_hadith = [taraf_hadith]
|
| 107 |
+
|
| 108 |
+
# Create a set to accumulate unique elements
|
| 109 |
+
unique_elements = {elem for key in taraf_hadith for elem in hadith_lookup[key]}
|
| 110 |
+
|
| 111 |
+
# Convert the set to a list for consistency
|
| 112 |
+
return list(unique_elements)
|
| 113 |
+
|
| 114 |
|
| 115 |
def visualize_isnad(taraf_num, yaxis):
|
| 116 |
# Precompute filtered dataframes
|
|
|
|
| 126 |
|
| 127 |
for i, hadith_parts in enumerate(taraf_hadith):
|
| 128 |
# look up hadith for each bookid_hadithid
|
| 129 |
+
isnad_hadith1 = isnad_info.iloc[lookup_hadith(taraf_hadith[i], HADITH_LOOKUP)][['Source', 'Destination']]
|
| 130 |
|
| 131 |
# Create graph and find end nodes
|
| 132 |
G = nx.from_pandas_edgelist(isnad_hadith1, source='Source', target='Destination', create_using=nx.DiGraph())
|
|
|
|
| 241 |
taraf['Index'] = np.arange(num_hadith)
|
| 242 |
sub_taraf = taraf[taraf['Index'].isin(hadiths)]
|
| 243 |
|
| 244 |
+
isnad_hadith = isnad_info.iloc[lookup_hadith(taraf_hadith, HADITH_LOOKUP)]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(int)
|
| 246 |
|
| 247 |
# Merge isnad_hadith with narrator_bios for Teacher and Student
|
|
|
|
| 286 |
df['bookid_hadithid'] = df['Book_ID'].astype(str) + '_' + df['Hadith Number'].astype(str)
|
| 287 |
hadith = matn_info[matn_info['bookid_hadithid'].isin(df['bookid_hadithid'])]
|
| 288 |
taraf_hadith = df['bookid_hadithid'].to_list()
|
| 289 |
+
isnad_hadith = isnad_info.iloc[lookup_hadith(taraf_hadith, HADITH_LOOKUP)]
|
|
|
|
|
|
|
|
|
|
| 290 |
isnad_hadith[['Source', 'Destination']] = isnad_hadith[['Source', 'Destination']].astype(int)
|
| 291 |
|
| 292 |
# Merge isnad_hadith with narrator_bios for Teacher and Student
|
|
|
|
| 344 |
|
| 345 |
# Process each hadith in taraf_hadith_split
|
| 346 |
for idx, split_hadith in enumerate(taraf_hadith):
|
| 347 |
+
isnad_hadith1 = isnad_info.iloc[lookup_hadith(taraf_hadith[i], HADITH_LOOKUP)]
|
| 348 |
G1 = nx.from_pandas_edgelist(isnad_hadith1, source='Source', target='Destination', create_using=nx.DiGraph())
|
| 349 |
if narrator in G1.nodes:
|
| 350 |
matns_with_narrator.append(taraf_hadith[idx])
|