Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from hazm import Normalizer, word_tokenize, Lemmatizer, POSTagger, Chunker, DependencyParser
|
| 3 |
+
|
| 4 |
+
# Initialize Hazm components
|
| 5 |
+
normalizer = Normalizer()
|
| 6 |
+
lemmatizer = Lemmatizer()
|
| 7 |
+
tagger = POSTagger(model='resources/postagger.model')
|
| 8 |
+
chunker = Chunker(model='resources/chunker.model')
|
| 9 |
+
parser = DependencyParser(tagger=tagger, lemmatizer=lemmatizer)
|
| 10 |
+
|
| 11 |
+
def process_text(text, operations):
|
| 12 |
+
result = {}
|
| 13 |
+
if 'normalize' in operations:
|
| 14 |
+
text = normalizer.normalize(text)
|
| 15 |
+
result['Normalized Text'] = text
|
| 16 |
+
if 'tokenize' in operations:
|
| 17 |
+
tokens = word_tokenize(text)
|
| 18 |
+
result['Tokens'] = tokens
|
| 19 |
+
if 'lemmatize' in operations:
|
| 20 |
+
lemmas = [lemmatizer.lemmatize(token) for token in word_tokenize(text)]
|
| 21 |
+
result['Lemmas'] = lemmas
|
| 22 |
+
if 'pos_tag' in operations:
|
| 23 |
+
pos_tags = tagger.tag(word_tokenize(text))
|
| 24 |
+
result['POS Tags'] = pos_tags
|
| 25 |
+
if 'chunk' in operations:
|
| 26 |
+
pos_tags = tagger.tag(word_tokenize(text))
|
| 27 |
+
chunks = chunker.parse(pos_tags)
|
| 28 |
+
result['Chunks'] = str(chunks)
|
| 29 |
+
if 'dependency_parse' in operations:
|
| 30 |
+
parse_tree = parser.parse(word_tokenize(text))
|
| 31 |
+
result['Dependency Parse'] = str(parse_tree)
|
| 32 |
+
return result
|
| 33 |
+
|
| 34 |
+
# Define Gradio interface
|
| 35 |
+
operations = ['normalize', 'tokenize', 'lemmatize', 'pos_tag', 'chunk', 'dependency_parse']
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=process_text,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.inputs.Textbox(lines=10, label="Input Text"),
|
| 40 |
+
gr.inputs.CheckboxGroup(operations, label="Operations")
|
| 41 |
+
],
|
| 42 |
+
outputs="json",
|
| 43 |
+
title="Persian Text Processor with Hazm",
|
| 44 |
+
description="Select operations to perform on the input text using Hazm."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
iface.launch()
|