Spaces:
Runtime error
Runtime error
Create pos_tagger.py
Browse files- pos_tagger.py +61 -0
pos_tagger.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from flair.data import Sentence
|
3 |
+
from flair.models import SequenceTagger
|
4 |
+
|
5 |
+
# Load the Flair model
|
6 |
+
model_path = "onurkeles/hamshetsnag-pos-tagger"
|
7 |
+
pos_tagger = SequenceTagger.load(model_path)
|
8 |
+
|
9 |
+
def tag_pos(text, detailed_output):
|
10 |
+
"""Tag parts of speech in a given text, with optional detailed output."""
|
11 |
+
sentence = Sentence(text)
|
12 |
+
pos_tagger.predict(sentence)
|
13 |
+
|
14 |
+
if detailed_output:
|
15 |
+
# Generate detailed information with tag values and probabilities
|
16 |
+
return "\n".join(
|
17 |
+
[f"{token.text}: {token.get_tag('pos').value} ({token.get_tag('pos').score:.2f})" for token in sentence]
|
18 |
+
)
|
19 |
+
else:
|
20 |
+
# Return a simple tagged string
|
21 |
+
return sentence.to_tagged_string()
|
22 |
+
|
23 |
+
def write():
|
24 |
+
st.markdown("# Part-of-Speech Tagging for Hamshetsnag")
|
25 |
+
st.sidebar.header("POS Tagging")
|
26 |
+
|
27 |
+
st.write(
|
28 |
+
'''Detect parts of speech in Hamshetsnag text using the fine-tuned model.'''
|
29 |
+
)
|
30 |
+
|
31 |
+
# Sidebar for configurations
|
32 |
+
st.sidebar.subheader("Configurable Parameters")
|
33 |
+
|
34 |
+
# Detailed Output Checkbox
|
35 |
+
detailed_output = st.sidebar.checkbox(
|
36 |
+
"Detailed Output",
|
37 |
+
value=False,
|
38 |
+
help="If checked, output shows detailed tag information (probability scores, etc.).",
|
39 |
+
)
|
40 |
+
|
41 |
+
# Input field for text
|
42 |
+
input_text = st.text_area(label='Enter a text: ', height=100, value="Put example text here.")
|
43 |
+
|
44 |
+
# Provide example sentences with translations
|
45 |
+
example_sentences = [
|
46 |
+
"tuute acertsetser topoldetser. aaav ta? (TR: Kâğıdı büzüştürdün attın. Oldu mu?)",
|
47 |
+
"Baran u Baden teran. (TR: Baran ve Bade koştu.)",
|
48 |
+
"Onurun ennush nu İremin terchushe intzi shad kızdırmısh aaav. (TR: Onur'un düşüşü ve İrem'in koşuşu beni kızdırdı.)"
|
49 |
+
]
|
50 |
+
|
51 |
+
st.write("## Example Sentences:")
|
52 |
+
for example in example_sentences:
|
53 |
+
if st.button(f"Use: {example.split('(TR:')[0].strip()}"):
|
54 |
+
input_text = example.split('(TR:')[0].strip() # Update the input text directly with the Hamshetsnag part
|
55 |
+
break # Only use the first clicked example
|
56 |
+
|
57 |
+
if st.button("Tag POS"):
|
58 |
+
with st.spinner('Processing...'):
|
59 |
+
# Tag the input text and format output as per settings
|
60 |
+
output = tag_pos(input_text, detailed_output)
|
61 |
+
st.success(output)
|