Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/doc#986 - Document best practice on main script #3

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 66 additions & 68 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,10 @@
import pandas as pd
from taipy.gui import Gui, notify

text = "Original text"

page = """
# Getting started with **Taipy**{: .color-primary} **GUI**{: .color-primary}

<|layout|columns=1 1|
<|
**My text:** <|{text}|>

**Enter a word:**
<|{text}|input|>
<|Analyze|button|on_action=local_callback|>
|>


<|Table|expandable|
<|{dataframe}|table|width=100%|number_format=%.2f|>
|>
|>

<|layout|columns=1 1 1|
## Positive <|{np.mean(dataframe['Score Pos'])}|text|format=%.2f|raw|>

## Neutral <|{np.mean(dataframe['Score Neu'])}|text|format=%.2f|raw|>

## Negative <|{np.mean(dataframe['Score Neg'])}|text|format=%.2f|raw|>
|>

<|{dataframe}|chart|type=bar|x=Text|y[1]=Score Pos|y[2]=Score Neu|y[3]=Score Neg|y[4]=Overall|color[1]=green|color[2]=grey|color[3]=red|type[4]=line|>
"""

MODEL = "sbcBI/sentiment_analysis_model"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForSequenceClassification.from_pretrained(MODEL)

dataframe = pd.DataFrame(
{
"Text": [""],
"Score Pos": [0.33],
"Score Neu": [0.33],
"Score Neg": [0.33],
"Overall": [0],
}
)

dataframe2 = dataframe.copy()


def analyze_text(input_text: str) -> dict:
"""
Expand Down Expand Up @@ -94,25 +51,6 @@ def local_callback(state) -> None:
state.text = ""


path = ""
treatment = 0

page_file = """
<|{path}|file_selector|extensions=.txt|label=Upload .txt file|on_action=analyze_file|> <|{f'Downloading {treatment}%...'}|>

<br/>

<|Table|expandable|
<|{dataframe2}|table|width=100%|number_format=%.2f|>
|>

<br/>

<|{dataframe2}|chart|type=bar|x=Text|y[1]=Score Pos|y[2]=Score Neu|y[3]=Score Neg|y[4]=Overall|color[1]=green|color[2]=grey|color[3]=red|type[4]=line|height=600px|>

"""


def analyze_file(state) -> None:
"""
Analyse the lines in a text file
Expand All @@ -138,11 +76,71 @@ def analyze_file(state) -> None:
state.path = None


pages = {
"/": "<|toggle|theme|>\n<center>\n<|navbar|>\n</center>",
"line": page,
"text": page_file,
}
if __name__ == "__main__":
text = "Original text"

page = """
# Getting started with **Taipy**{: .color-primary} **GUI**{: .color-primary}

<|layout|columns=1 1|
<|
**My text:** <|{text}|>

**Enter a word:**
<|{text}|input|>
<|Analyze|button|on_action=local_callback|>
|>


<|Table|expandable|
<|{dataframe}|table|width=100%|number_format=%.2f|>
|>
|>

<|layout|columns=1 1 1|
## Positive <|{np.mean(dataframe['Score Pos'])}|text|format=%.2f|raw|>

## Neutral <|{np.mean(dataframe['Score Neu'])}|text|format=%.2f|raw|>

## Negative <|{np.mean(dataframe['Score Neg'])}|text|format=%.2f|raw|>
|>

<|{dataframe}|chart|type=bar|x=Text|y[1]=Score Pos|y[2]=Score Neu|y[3]=Score Neg|y[4]=Overall|color[1]=green|color[2]=grey|color[3]=red|type[4]=line|>
"""

dataframe = pd.DataFrame(
{
"Text": [""],
"Score Pos": [0.33],
"Score Neu": [0.33],
"Score Neg": [0.33],
"Overall": [0],
}
)

dataframe2 = dataframe.copy()

path = ""
treatment = 0

page_file = """
<|{path}|file_selector|extensions=.txt|label=Upload .txt file|on_action=analyze_file|> <|{f'Downloading {treatment}%...'}|>

<br/>

<|Table|expandable|
<|{dataframe2}|table|width=100%|number_format=%.2f|>
|>

<br/>

<|{dataframe2}|chart|type=bar|x=Text|y[1]=Score Pos|y[2]=Score Neu|y[3]=Score Neg|y[4]=Overall|color[1]=green|color[2]=grey|color[3]=red|type[4]=line|height=600px|>
"""

pages = {
"/": "<|toggle|theme|>\n<center>\n<|navbar|>\n</center>",
"line": page,
"text": page_file,
}

Gui(pages=pages).run(title="Sentiment Analysis")
Gui(pages=pages).run(title="Sentiment Analysis")