Spaces:
Sleeping
Sleeping
initial code update
Browse files
app.py
CHANGED
|
@@ -6,12 +6,12 @@ import os
|
|
| 6 |
import pandas as pd
|
| 7 |
import re
|
| 8 |
|
| 9 |
-
#models
|
| 10 |
from transformers import AutoTokenizer, AutoModelForTokenClassification,AutoModelForSequenceClassification, pipeline
|
| 11 |
import torch
|
| 12 |
|
| 13 |
aspdevice = "cpu"
|
| 14 |
-
|
| 15 |
model_id_ate = "gauneg/roberta-base-absa-ate-sentiment"
|
| 16 |
tokenizer_ate = AutoTokenizer.from_pretrained(model_id_ate)
|
| 17 |
model_ate = AutoModelForTokenClassification.from_pretrained(model_id_ate)
|
|
@@ -67,9 +67,7 @@ def extract_full_analysis(review):
|
|
| 67 |
}
|
| 68 |
return dynamic_result
|
| 69 |
|
| 70 |
-
|
| 71 |
-
with open('yelp3k.json', 'r', encoding='utf-8') as fp:
|
| 72 |
-
static_reviews = json.load(fp)
|
| 73 |
def highlight_aspects(text, aspects):
|
| 74 |
aspects_sorted = sorted(aspects, key=len, reverse=True)
|
| 75 |
aspects_regex = [re.escape(asp) for asp in aspects_sorted]
|
|
@@ -102,38 +100,11 @@ def format_review(review):
|
|
| 102 |
{aspects_html}
|
| 103 |
<h3>Sentiments</h3>
|
| 104 |
{sentiments_html}
|
| 105 |
-
<h3>Sentiments Score</h3>
|
| 106 |
-
{sentiScor_html}
|
| 107 |
-
|
| 108 |
<h3>Emotions</h3>
|
| 109 |
{emotions_html}
|
| 110 |
-
<h3>Emotions Score</h3>
|
| 111 |
-
{emoScor_html}
|
| 112 |
-
|
| 113 |
</div>
|
| 114 |
"""
|
| 115 |
|
| 116 |
-
# Format dynamic reviews
|
| 117 |
-
def format_dynamic_reviews(reviews):
|
| 118 |
-
return "<div>" + "".join(format_review(rev) for rev in reviews) + "</div>"
|
| 119 |
-
|
| 120 |
-
def get_static_review(index):
|
| 121 |
-
review = static_reviews[index]
|
| 122 |
-
highlighted_text = highlight_aspects(review['text'], review['aspect_word'].split(','))
|
| 123 |
-
stars_html = f"Stars: {review['stars']}"
|
| 124 |
-
aspects = [asp.strip() for asp in review['aspect_word'].split(',')]
|
| 125 |
-
emotions = [emo.strip() for emo in review['emotion'].split(',')]
|
| 126 |
-
|
| 127 |
-
aspects_html = '<h3>Aspect Words</h3><div class="cell-grid">' + "".join(f'<div class="cell-item">{html.escape(asp)}</div>' for asp in aspects) + '</div>'
|
| 128 |
-
emotions_html = '<h3>Emotions Extracted</h3><div class="cell-grid">' + "".join(f'<div class="cell-item">{html.escape(emo)}</div>' for emo in emotions) + '</div>'
|
| 129 |
-
|
| 130 |
-
info_text = f"Review {index+1} of {len(static_reviews)}"
|
| 131 |
-
return highlighted_text, stars_html, aspects_html, emotions_html, info_text
|
| 132 |
-
|
| 133 |
-
def navigate(direction, current_index):
|
| 134 |
-
if direction == 'prev':
|
| 135 |
-
return max(current_index - 1, 0)
|
| 136 |
-
return min(current_index + 1, len(static_reviews) - 1)
|
| 137 |
|
| 138 |
def submit_new_review(text):
|
| 139 |
if not text.strip():
|
|
@@ -159,19 +130,6 @@ with gr.Blocks(css="""
|
|
| 159 |
# Header
|
| 160 |
gr.Markdown("# Yelp Review Demonstration for Aspect and Emotion Extracted")
|
| 161 |
|
| 162 |
-
# Static Reviews
|
| 163 |
-
with gr.Row():
|
| 164 |
-
with gr.Column(scale=11):
|
| 165 |
-
static_text = gr.HTML(label="Review Text")
|
| 166 |
-
with gr.Column(scale=1):
|
| 167 |
-
static_stars = gr.HTML(label="Stars")
|
| 168 |
-
static_aspects = gr.HTML(label=None)
|
| 169 |
-
static_emotions = gr.HTML(label="Emotions")
|
| 170 |
-
static_info = gr.Textbox(label="Review Info", interactive=False)
|
| 171 |
-
with gr.Row():
|
| 172 |
-
prev_button = gr.Button("Previous")
|
| 173 |
-
next_button = gr.Button("Next")
|
| 174 |
-
|
| 175 |
# Submit Form
|
| 176 |
gr.Markdown("## Submit Your Review")
|
| 177 |
with gr.Row():
|
|
@@ -184,21 +142,10 @@ with gr.Blocks(css="""
|
|
| 184 |
dynamic_display = gr.HTML()
|
| 185 |
|
| 186 |
# States
|
| 187 |
-
current_static_index = gr.State(0)
|
| 188 |
dynamic_reviews_state = gr.State()
|
| 189 |
-
|
| 190 |
-
# Load Initial Data
|
| 191 |
-
demo.load(get_static_review, inputs=current_static_index, outputs=[static_text, static_stars, static_aspects, static_emotions, static_info])
|
| 192 |
-
|
| 193 |
-
# Event Handlers
|
| 194 |
-
prev_button.click(lambda ci: navigate('prev', ci), inputs=current_static_index, outputs=current_static_index).then(
|
| 195 |
-
get_static_review, inputs=current_static_index, outputs=[static_text, static_stars, static_aspects, static_emotions, static_info])
|
| 196 |
-
next_button.click(lambda ci: navigate('next', ci), inputs=current_static_index, outputs=current_static_index).then(
|
| 197 |
-
get_static_review, inputs=current_static_index, outputs=[static_text, static_stars, static_aspects, static_emotions, static_info])
|
| 198 |
submit_button.click(submit_new_review, inputs=submit_text, outputs=[dynamic_display, dynamic_reviews_state])
|
| 199 |
|
| 200 |
# Launch
|
| 201 |
if __name__ == "__main__":
|
| 202 |
-
|
| 203 |
-
demo.launch(server_name="0.0.0.0", server_port=port,share=True)
|
| 204 |
# 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
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
import re
|
| 8 |
|
| 9 |
+
# models
|
| 10 |
from transformers import AutoTokenizer, AutoModelForTokenClassification,AutoModelForSequenceClassification, pipeline
|
| 11 |
import torch
|
| 12 |
|
| 13 |
aspdevice = "cpu"
|
| 14 |
+
# Your aspect extraction logic
|
| 15 |
model_id_ate = "gauneg/roberta-base-absa-ate-sentiment"
|
| 16 |
tokenizer_ate = AutoTokenizer.from_pretrained(model_id_ate)
|
| 17 |
model_ate = AutoModelForTokenClassification.from_pretrained(model_id_ate)
|
|
|
|
| 67 |
}
|
| 68 |
return dynamic_result
|
| 69 |
|
| 70 |
+
|
|
|
|
|
|
|
| 71 |
def highlight_aspects(text, aspects):
|
| 72 |
aspects_sorted = sorted(aspects, key=len, reverse=True)
|
| 73 |
aspects_regex = [re.escape(asp) for asp in aspects_sorted]
|
|
|
|
| 100 |
{aspects_html}
|
| 101 |
<h3>Sentiments</h3>
|
| 102 |
{sentiments_html}
|
|
|
|
|
|
|
|
|
|
| 103 |
<h3>Emotions</h3>
|
| 104 |
{emotions_html}
|
|
|
|
|
|
|
|
|
|
| 105 |
</div>
|
| 106 |
"""
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
def submit_new_review(text):
|
| 110 |
if not text.strip():
|
|
|
|
| 130 |
# Header
|
| 131 |
gr.Markdown("# Yelp Review Demonstration for Aspect and Emotion Extracted")
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
# Submit Form
|
| 134 |
gr.Markdown("## Submit Your Review")
|
| 135 |
with gr.Row():
|
|
|
|
| 142 |
dynamic_display = gr.HTML()
|
| 143 |
|
| 144 |
# States
|
|
|
|
| 145 |
dynamic_reviews_state = gr.State()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
submit_button.click(submit_new_review, inputs=submit_text, outputs=[dynamic_display, dynamic_reviews_state])
|
| 147 |
|
| 148 |
# Launch
|
| 149 |
if __name__ == "__main__":
|
| 150 |
+
demo.launch()
|
|
|
|
| 151 |
# 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
|