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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
| 1 |
+
# app.py - Main Gradio application file
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import pickle
|
| 5 |
+
import os
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from typing import Dict, List, Tuple, Any
|
| 9 |
|
| 10 |
+
class GreekMorphosyntacticParser:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.model_repo = "sbompolas/Lesbian-Greek-Morphosyntactic-Model"
|
| 13 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
|
| 15 |
+
# Model components
|
| 16 |
+
self.tokenizer = None
|
| 17 |
+
self.lemmatizer = None
|
| 18 |
+
self.parser = None
|
| 19 |
+
self.tagger = None
|
| 20 |
+
|
| 21 |
+
# Load models
|
| 22 |
+
self.load_models()
|
| 23 |
+
|
| 24 |
+
def load_models(self):
|
| 25 |
+
"""Load all model components from Hugging Face Hub"""
|
| 26 |
+
try:
|
| 27 |
+
print("Loading tokenizer...")
|
| 28 |
+
tokenizer_path = hf_hub_download(
|
| 29 |
+
repo_id=self.model_repo,
|
| 30 |
+
filename="el_test_tokenizer.pt",
|
| 31 |
+
cache_dir="./models"
|
| 32 |
+
)
|
| 33 |
+
with open(tokenizer_path, 'rb') as f:
|
| 34 |
+
self.tokenizer = pickle.load(f)
|
| 35 |
+
|
| 36 |
+
print("Loading lemmatizer...")
|
| 37 |
+
lemmatizer_path = hf_hub_download(
|
| 38 |
+
repo_id=self.model_repo,
|
| 39 |
+
filename="el_test_nocharlm_lemmatizer.pt",
|
| 40 |
+
cache_dir="./models"
|
| 41 |
+
)
|
| 42 |
+
with open(lemmatizer_path, 'rb') as f:
|
| 43 |
+
self.lemmatizer = pickle.load(f)
|
| 44 |
+
|
| 45 |
+
print("Loading parser...")
|
| 46 |
+
parser_path = hf_hub_download(
|
| 47 |
+
repo_id=self.model_repo,
|
| 48 |
+
filename="el_test_transformer_parser.pt",
|
| 49 |
+
cache_dir="./models"
|
| 50 |
+
)
|
| 51 |
+
self.parser = torch.load(parser_path, map_location=self.device)
|
| 52 |
+
|
| 53 |
+
print("Loading tagger...")
|
| 54 |
+
tagger_path = hf_hub_download(
|
| 55 |
+
repo_id=self.model_repo,
|
| 56 |
+
filename="el_test_transformer_tagger.pt",
|
| 57 |
+
cache_dir="./models"
|
| 58 |
+
)
|
| 59 |
+
self.tagger = torch.load(tagger_path, map_location=self.device)
|
| 60 |
+
|
| 61 |
+
# Move models to device
|
| 62 |
+
if hasattr(self.parser, 'to'):
|
| 63 |
+
self.parser.to(self.device)
|
| 64 |
+
if hasattr(self.tagger, 'to'):
|
| 65 |
+
self.tagger.to(self.device)
|
| 66 |
+
|
| 67 |
+
print("All models loaded successfully!")
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Error loading models: {e}")
|
| 71 |
+
raise e
|
| 72 |
+
|
| 73 |
+
def tokenize_text(self, text: str) -> List[str]:
|
| 74 |
+
"""Tokenize input text"""
|
| 75 |
+
if self.tokenizer is None:
|
| 76 |
+
raise ValueError("Tokenizer not loaded")
|
| 77 |
+
|
| 78 |
+
# Basic tokenization - adjust based on actual tokenizer interface
|
| 79 |
+
if hasattr(self.tokenizer, 'tokenize'):
|
| 80 |
+
tokens = self.tokenizer.tokenize(text)
|
| 81 |
+
elif hasattr(self.tokenizer, '__call__'):
|
| 82 |
+
tokens = self.tokenizer(text)
|
| 83 |
+
else:
|
| 84 |
+
# Fallback to simple whitespace tokenization
|
| 85 |
+
tokens = text.split()
|
| 86 |
+
|
| 87 |
+
return tokens
|
| 88 |
+
|
| 89 |
+
def get_morphology(self, tokens: List[str]) -> List[Dict[str, Any]]:
|
| 90 |
+
"""Get morphological analysis for tokens"""
|
| 91 |
+
if self.tagger is None:
|
| 92 |
+
raise ValueError("Tagger not loaded")
|
| 93 |
+
|
| 94 |
+
morphology = []
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
# Convert tokens to tensor if needed
|
| 98 |
+
if hasattr(self.tagger, 'predict'):
|
| 99 |
+
predictions = self.tagger.predict(tokens)
|
| 100 |
+
else:
|
| 101 |
+
# Implement prediction logic based on model architecture
|
| 102 |
+
with torch.no_grad():
|
| 103 |
+
# This is a placeholder - actual implementation depends on model interface
|
| 104 |
+
predictions = ["NOUN" for _ in tokens] # Fallback
|
| 105 |
+
|
| 106 |
+
for i, token in enumerate(tokens):
|
| 107 |
+
morphology.append({
|
| 108 |
+
'token': token,
|
| 109 |
+
'pos': predictions[i] if i < len(predictions) else "UNK",
|
| 110 |
+
'features': {} # Add morphological features if available
|
| 111 |
+
})
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"Error in morphological analysis: {e}")
|
| 115 |
+
# Fallback morphology
|
| 116 |
+
for token in tokens:
|
| 117 |
+
morphology.append({
|
| 118 |
+
'token': token,
|
| 119 |
+
'pos': "UNK",
|
| 120 |
+
'features': {}
|
| 121 |
+
})
|
| 122 |
+
|
| 123 |
+
return morphology
|
| 124 |
+
|
| 125 |
+
def get_lemmas(self, tokens: List[str]) -> List[str]:
|
| 126 |
+
"""Get lemmas for tokens"""
|
| 127 |
+
if self.lemmatizer is None:
|
| 128 |
+
raise ValueError("Lemmatizer not loaded")
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
if hasattr(self.lemmatizer, 'lemmatize'):
|
| 132 |
+
lemmas = [self.lemmatizer.lemmatize(token) for token in tokens]
|
| 133 |
+
elif hasattr(self.lemmatizer, '__call__'):
|
| 134 |
+
lemmas = self.lemmatizer(tokens)
|
| 135 |
+
else:
|
| 136 |
+
# Fallback
|
| 137 |
+
lemmas = tokens # Return original tokens as fallback
|
| 138 |
+
|
| 139 |
+
return lemmas
|
| 140 |
+
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f"Error in lemmatization: {e}")
|
| 143 |
+
return tokens # Return original tokens as fallback
|
| 144 |
+
|
| 145 |
+
def get_syntax(self, tokens: List[str]) -> List[Tuple[int, str, int]]:
|
| 146 |
+
"""Get syntactic dependencies"""
|
| 147 |
+
if self.parser is None:
|
| 148 |
+
raise ValueError("Parser not loaded")
|
| 149 |
+
|
| 150 |
+
try:
|
| 151 |
+
# Implement parsing logic based on model architecture
|
| 152 |
+
dependencies = []
|
| 153 |
+
|
| 154 |
+
if hasattr(self.parser, 'parse'):
|
| 155 |
+
parse_result = self.parser.parse(tokens)
|
| 156 |
+
dependencies = parse_result
|
| 157 |
+
else:
|
| 158 |
+
# Fallback dependencies (simple linear structure)
|
| 159 |
+
for i, token in enumerate(tokens):
|
| 160 |
+
head = i - 1 if i > 0 else 0
|
| 161 |
+
relation = "dep"
|
| 162 |
+
dependencies.append((i, relation, head))
|
| 163 |
+
|
| 164 |
+
return dependencies
|
| 165 |
+
|
| 166 |
+
except Exception as e:
|
| 167 |
+
print(f"Error in syntactic parsing: {e}")
|
| 168 |
+
# Fallback dependencies
|
| 169 |
+
dependencies = []
|
| 170 |
+
for i, token in enumerate(tokens):
|
| 171 |
+
head = i - 1 if i > 0 else 0
|
| 172 |
+
relation = "dep"
|
| 173 |
+
dependencies.append((i, relation, head))
|
| 174 |
+
return dependencies
|
| 175 |
+
|
| 176 |
+
def parse_text(self, text: str) -> Dict[str, Any]:
|
| 177 |
+
"""Complete morphosyntactic analysis of input text"""
|
| 178 |
+
if not text.strip():
|
| 179 |
+
return {"error": "Please enter some text to parse"}
|
| 180 |
+
|
| 181 |
+
try:
|
| 182 |
+
# Tokenization
|
| 183 |
+
tokens = self.tokenize_text(text)
|
| 184 |
+
|
| 185 |
+
# Morphological analysis
|
| 186 |
+
morphology = self.get_morphology(tokens)
|
| 187 |
+
|
| 188 |
+
# Lemmatization
|
| 189 |
+
lemmas = self.get_lemmas(tokens)
|
| 190 |
+
|
| 191 |
+
# Syntactic parsing
|
| 192 |
+
dependencies = self.get_syntax(tokens)
|
| 193 |
+
|
| 194 |
+
# Combine results
|
| 195 |
+
results = []
|
| 196 |
+
for i, token in enumerate(tokens):
|
| 197 |
+
lemma = lemmas[i] if i < len(lemmas) else token
|
| 198 |
+
morph = morphology[i] if i < len(morphology) else {'pos': 'UNK', 'features': {}}
|
| 199 |
+
|
| 200 |
+
# Find dependency info
|
| 201 |
+
dep_info = None
|
| 202 |
+
for dep in dependencies:
|
| 203 |
+
if dep[0] == i:
|
| 204 |
+
dep_info = dep
|
| 205 |
+
break
|
| 206 |
+
|
| 207 |
+
result = {
|
| 208 |
+
'id': i + 1,
|
| 209 |
+
'token': token,
|
| 210 |
+
'lemma': lemma,
|
| 211 |
+
'pos': morph.get('pos', 'UNK'),
|
| 212 |
+
'features': morph.get('features', {}),
|
| 213 |
+
'head': dep_info[2] + 1 if dep_info else 0,
|
| 214 |
+
'relation': dep_info[1] if dep_info else 'root'
|
| 215 |
+
}
|
| 216 |
+
results.append(result)
|
| 217 |
+
|
| 218 |
+
return {
|
| 219 |
+
'success': True,
|
| 220 |
+
'tokens': len(tokens),
|
| 221 |
+
'analysis': results
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
except Exception as e:
|
| 225 |
+
return {"error": f"Error during parsing: {str(e)}"}
|
| 226 |
|
| 227 |
+
# Initialize parser
|
| 228 |
+
try:
|
| 229 |
+
parser = GreekMorphosyntacticParser()
|
| 230 |
+
parser_loaded = True
|
| 231 |
+
except Exception as e:
|
| 232 |
+
print(f"Failed to initialize parser: {e}")
|
| 233 |
+
parser_loaded = False
|
| 234 |
+
parser = None
|
| 235 |
|
| 236 |
+
def parse_greek_text(text: str):
|
| 237 |
+
"""Gradio interface function"""
|
| 238 |
+
if not parser_loaded:
|
| 239 |
+
return "❌ Error: Models failed to load. Please check the logs.", None
|
| 240 |
+
|
| 241 |
+
if not text.strip():
|
| 242 |
+
return "⚠️ Please enter some Greek text to analyze.", None
|
| 243 |
+
|
| 244 |
+
result = parser.parse_text(text)
|
| 245 |
+
|
| 246 |
+
if "error" in result:
|
| 247 |
+
return f"❌ {result['error']}", None
|
| 248 |
+
|
| 249 |
+
# Format results for display
|
| 250 |
+
analysis = result['analysis']
|
| 251 |
+
|
| 252 |
+
# Create formatted output
|
| 253 |
+
output_text = f"📊 **Analysis Results** ({result['tokens']} tokens)\n\n"
|
| 254 |
+
|
| 255 |
+
# Create table data
|
| 256 |
+
table_data = []
|
| 257 |
+
for item in analysis:
|
| 258 |
+
features_str = ", ".join([f"{k}={v}" for k, v in item['features'].items()]) if item['features'] else "-"
|
| 259 |
+
table_data.append([
|
| 260 |
+
item['id'],
|
| 261 |
+
item['token'],
|
| 262 |
+
item['lemma'],
|
| 263 |
+
item['pos'],
|
| 264 |
+
features_str,
|
| 265 |
+
item['head'],
|
| 266 |
+
item['relation']
|
| 267 |
+
])
|
| 268 |
+
|
| 269 |
+
# Create DataFrame for better display
|
| 270 |
+
df = pd.DataFrame(table_data, columns=[
|
| 271 |
+
'ID', 'Token', 'Lemma', 'POS', 'Features', 'Head', 'Relation'
|
| 272 |
+
])
|
| 273 |
+
|
| 274 |
+
return output_text, df
|
| 275 |
|
| 276 |
+
def create_interface():
|
| 277 |
+
"""Create Gradio interface"""
|
| 278 |
+
with gr.Blocks(
|
| 279 |
+
title="Greek Morphosyntactic Parser",
|
| 280 |
+
theme=gr.themes.Soft(),
|
| 281 |
+
) as demo:
|
| 282 |
+
|
| 283 |
+
gr.Markdown("""
|
| 284 |
+
# 🏛️ Ancient Greek Morphosyntactic Parser
|
| 285 |
+
|
| 286 |
+
This tool uses the **Lesbian Greek Morphosyntactic Model** to analyze Ancient Greek text.
|
| 287 |
+
It provides:
|
| 288 |
+
- **Tokenization**: Breaking text into individual words
|
| 289 |
+
- **Lemmatization**: Finding the dictionary form of words
|
| 290 |
+
- **POS Tagging**: Identifying parts of speech
|
| 291 |
+
- **Morphological Analysis**: Analyzing grammatical features
|
| 292 |
+
- **Dependency Parsing**: Finding syntactic relationships
|
| 293 |
+
|
| 294 |
+
## How to use:
|
| 295 |
+
1. Enter your Ancient Greek text in the input box
|
| 296 |
+
2. Click "Parse Text" to analyze
|
| 297 |
+
3. View the results in the table below
|
| 298 |
+
""")
|
| 299 |
+
|
| 300 |
+
with gr.Row():
|
| 301 |
+
with gr.Column(scale=2):
|
| 302 |
+
input_text = gr.Textbox(
|
| 303 |
+
label="Ancient Greek Text",
|
| 304 |
+
placeholder="Enter your Ancient Greek text here...",
|
| 305 |
+
lines=5,
|
| 306 |
+
max_lines=10
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
parse_btn = gr.Button(
|
| 310 |
+
"🔍 Parse Text",
|
| 311 |
+
variant="primary",
|
| 312 |
+
size="lg"
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
with gr.Column(scale=1):
|
| 316 |
+
gr.Markdown("""
|
| 317 |
+
### Example Texts:
|
| 318 |
+
|
| 319 |
+
Try these example phrases:
|
| 320 |
+
|
| 321 |
+
**Epic/Homeric:**
|
| 322 |
+
- μῆνιν ἄειδε θεὰ Πηληϊάδεω Ἀχιλῆος
|
| 323 |
+
|
| 324 |
+
**Classical:**
|
| 325 |
+
- γνῶθι σεαυτόν
|
| 326 |
+
- πάντων χρημάτων μέτρον ἄνθρωπος
|
| 327 |
+
|
| 328 |
+
**Lyric (Sapphic):**
|
| 329 |
+
- φαίνεταί μοι κῆνος ἴσος θέοισιν
|
| 330 |
+
""")
|
| 331 |
+
|
| 332 |
+
with gr.Row():
|
| 333 |
+
output_text = gr.Markdown(label="Analysis Summary")
|
| 334 |
+
|
| 335 |
+
with gr.Row():
|
| 336 |
+
output_table = gr.Dataframe(
|
| 337 |
+
label="Detailed Analysis",
|
| 338 |
+
headers=['ID', 'Token', 'Lemma', 'POS', 'Features', 'Head', 'Relation'],
|
| 339 |
+
datatype=['number', 'str', 'str', 'str', 'str', 'number', 'str'],
|
| 340 |
+
interactive=False
|
| 341 |
+
)
|
| 342 |
+
|
| 343 |
+
# Event handlers
|
| 344 |
+
parse_btn.click(
|
| 345 |
+
fn=parse_greek_text,
|
| 346 |
+
inputs=[input_text],
|
| 347 |
+
outputs=[output_text, output_table]
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
input_text.submit(
|
| 351 |
+
fn=parse_greek_text,
|
| 352 |
+
inputs=[input_text],
|
| 353 |
+
outputs=[output_text, output_table]
|
| 354 |
+
)
|
| 355 |
+
|
| 356 |
+
# Footer
|
| 357 |
+
gr.Markdown("""
|
| 358 |
+
---
|
| 359 |
+
**Model:** [sbompolas/Lesbian-Greek-Morphosyntactic-Model](https://huggingface.co/sbompolas/Lesbian-Greek-Morphosyntactic-Model)
|
| 360 |
+
|
| 361 |
+
**Note:** This model is specifically trained for Ancient Greek morphosyntactic analysis.
|
| 362 |
+
Results may vary depending on the dialect and time period of your input text.
|
| 363 |
+
""")
|
| 364 |
+
|
| 365 |
+
return demo
|
| 366 |
|
| 367 |
+
if __name__ == "__main__":
|
| 368 |
+
# Create and launch interface
|
| 369 |
+
demo = create_interface()
|
| 370 |
+
demo.launch(
|
| 371 |
+
server_name="0.0.0.0",
|
| 372 |
+
server_port=7860,
|
| 373 |
+
share=True
|
| 374 |
+
)
|