Spaces:
Sleeping
Sleeping
import gradio as gr | |
from utils import T5Generator | |
# Preprocessing function that modifies the user input. | |
def preprocess_input(user_input): | |
bos_instruction_id = """Definition: The output will be the aspects (both implicit and explicit) and the aspect's sentiment polarity. In cases where there are no aspects the output should be noaspectterm:none. | |
Examples: | |
1. | |
input: The hotel was beautiful, with friendly staff and a delicious breakfast buffet. However, the Wi-Fi was slow, the bathroom wasn't clean, and the room had a weird smell. | |
output: hotel\:positive, staff\:positive, breakfast buffet\:positive, Wi-Fi\:negative, bathroom\:negative, room smell\:negative | |
2. | |
input: This coat looks stylish and fits perfectly. The fabric feels high-quality, and the color is vibrant. But the buttons fall off easily, and it's not warm enough for winter. | |
output: style\:positive, fit\:positive, fabric\:positive, color\:positive, buttons\:negative, warmth\:negative | |
3. | |
input: The online course was affordable and the instructor explained topics clearly. However, the video quality was poor, the quizzes were too simple, and the pacing was inconsistent. | |
output: cost\:positive, instructor clarity\:positive, video quality\:negative, quizzes\:negative, pacing\:negative | |
4. | |
input: My experience with this bank has been mostly good. The mobile app is user-friendly, staff are polite, and transactions are smooth. But the customer support takes too long to respond, and hidden fees are frustrating. | |
output: mobile app\:positive, staff\:positive, transactions\:positive, customer support\:negative, hidden fees\:negative | |
5. | |
input: I enjoyed the plot and the character development in this novel. The writing style was smooth, and the dialogue felt real. But the ending was disappointing and the pacing was uneven in the middle chapters. | |
output: plot\:positive, character development\:positive, writing style\:positive, dialogue\:positive, ending\:negative, pacing\:negative | |
6. | |
input: The doctor was kind and explained everything well. The clinic was clean, and the staff was professional. But the waiting time was too long, and I felt rushed during the consultation. | |
output: doctor\:positive, clinic cleanliness\:positive, staff professionalism\:positive, waiting time\:negative, consultation time\:negative | |
7. | |
input: The sound quality of these headphones is amazing, and the battery lasts long. The design is sleek, and the noise cancellation works well. But they’re uncomfortable after long use and overpriced. | |
output: sound quality\:positive, battery life\:positive, design\:positive, noise cancellation\:positive, comfort\:negative, price\:negative | |
8. | |
input: This vacuum cleaner has strong suction power and is easy to maneuver. It’s lightweight and the attachments are useful. However, it's very loud and the dustbin fills up quickly. | |
output: suction power\:positive, maneuverability\:positive, weight\:positive, attachments\:positive, noise\:negative, dustbin size\:negative | |
9. | |
input: The recipe was easy to follow, and the dish turned out flavorful. The ingredients were accessible, and the cooking time was just right. But the portion size was small and cleanup took a while. | |
output: instructions\:positive, taste\:positive, ingredients\:positive, cooking time\:positive, portion size\:negative, cleanup\:negative | |
10. | |
input: This gym is well-equipped and spacious. The trainers are knowledgeable and the membership is reasonably priced. However, the showers are often dirty and there’s limited parking. | |
output: equipment\:positive, space\:positive, trainers\:positive, price\:positive, showers\:negative, parking\:negative | |
11. | |
input: The flight was on time, and the in-flight entertainment was great. Seats were clean and staff were courteous. But the legroom was terrible and the food was tasteless. | |
output: punctuality\:positive, entertainment\:positive, cleanliness\:positive, staff\:positive, legroom\:negative, food\:negative | |
12. | |
input: The mobile phone's camera is sharp, the screen is vibrant, and the battery life is impressive. However, the device heats up quickly and the UI is cluttered. | |
output: camera\:positive, screen\:positive, battery\:positive, heating\:negative, UI\:negative | |
13. | |
input: The tour guide was informative, and the destinations were breathtaking. The transportation was comfortable and well organized. But meals were mediocre and the schedule was too packed. | |
output: guide\:positive, destinations\:positive, transportation\:positive, meals\:negative, schedule\:negative | |
14. | |
input: The jeans fit perfectly, are very comfortable, and the material feels premium. However, the dye runs in the wash and delivery took too long. | |
output: fit\:positive, comfort\:positive, material\:positive, dye quality\:negative, delivery\:negative | |
15. | |
input: This smartwatch tracks fitness accurately, has a responsive touch screen, and looks stylish. But syncing with the phone is inconsistent and charging takes too long. | |
output: fitness tracking\:positive, touchscreen\:positive, design\:positive, syncing\:negative, charging\:negative | |
16. | |
input: The therapist was compassionate and helpful. Sessions were structured and impactful. However, the cost was high, and appointment availability was limited. | |
output: therapist\:positive, session structure\:positive, impact\:positive, cost\:negative, appointment availability\:negative | |
17. | |
input: The gaming console runs smoothly and the graphics are top-notch. The game library is vast, and loading times are short. But the controllers feel cheap and it's very bulky. | |
output: performance\:positive, graphics\:positive, game library\:positive, loading time\:positive, controller quality\:negative, size\:negative | |
18. | |
input: This online store has a great variety of products and fast delivery. Prices are competitive and the website is easy to navigate. But the return process is tedious and customer support is unhelpful. | |
output: product variety\:positive, delivery\:positive, pricing\:positive, website UX\:positive, return process\:negative, customer support\:negative | |
19. | |
input: The air conditioner cools quickly, has a quiet mode, and looks modern. But the installation service was poor and the remote is hard to use. | |
output: cooling\:positive, noise\:positive, design\:positive, installation\:negative, remote\:negative | |
20. | |
input: The blender is powerful, easy to clean, and comes with useful attachments. But it's very noisy and the lid is hard to remove. | |
output: power\:positive, cleaning\:positive, attachments\:positive, noise\:negative, lid\:negative | |
21. | |
input: The drama had amazing cinematography and a compelling storyline. The acting was strong and the soundtrack was fitting. But the pacing was off and the ending felt rushed. | |
output: cinematography\:positive, storyline\:positive, acting\:positive, soundtrack\:positive, pacing\:negative, ending\:negative | |
22. | |
input: The mattress is very comfortable and supports my back well. It’s breathable and has no chemical smell. However, delivery was delayed and the warranty terms are confusing. | |
output: comfort\:positive, support\:positive, breathability\:positive, smell\:positive, delivery\:negative, warranty\:negative | |
23. | |
input: The mobile app is intuitive and has many features. Login is quick and data syncs well. However, it crashes often and has annoying ads. | |
output: usability\:positive, features\:positive, login\:positive, sync\:positive, stability\:negative, ads\:negative | |
24. | |
input: The watch looks elegant, keeps time perfectly, and feels solid. But the strap is uncomfortable and it scratches too easily. | |
output: design\:positive, accuracy\:positive, build\:positive, strap comfort\:negative, durability\:negative | |
25. | |
input: The refrigerator is spacious and energy-efficient. It’s quiet and cools evenly. But the freezer section is small and the shelves feel flimsy. | |
output: capacity\:positive, efficiency\:positive, noise\:positive, cooling\:positive, freezer\:negative, shelf quality\:negative | |
26. | |
input: The coffee maker brews quickly and the coffee is hot. The design is sleek, and it's easy to clean. However, it leaks when brewing and is louder than expected. | |
output: brewing time\:positive, coffee temperature\:positive, design\:positive, cleaning\:positive, leakage\:negative, noise\:negative | |
27. | |
input: The sports shoes are comfortable, durable, and stylish. The grip is excellent, and they are lightweight. But the stitching came undone after a few uses, and the price is too high. | |
output: comfort\:positive, durability\:positive, style\:positive, grip\:positive, stitching\:negative, price\:negative | |
28. | |
input: The smartwatch tracks steps accurately and has a great battery life. It's sleek and easy to use. But the heart rate monitor is inaccurate and the band is uncomfortable. | |
output: step tracking\:positive, battery life\:positive, design\:positive, ease of use\:positive, heart rate monitor\:negative, band comfort\:negative | |
29. | |
input: The pizza crust was crispy, and the toppings were fresh. The sauce was flavorful, and the cheese was perfectly melted. However, it was too greasy and the delivery was late. | |
output: crust\:positive, toppings\:positive, sauce\:positive, cheese\:positive, grease\:negative, delivery time\:negative | |
30. | |
input: The tablet has a sharp display and the interface is responsive. It’s lightweight and portable. But the battery drains too quickly, and the camera quality is poor. | |
output: display\:positive, interface\:positive, portability\:positive, battery life\:negative, camera\:negative | |
31. | |
input: The hotel room had a great view and was spacious. The bed was comfortable, and the bathroom was well-equipped. However, the room service was slow, and the TV had limited channels. | |
output: view\:positive, space\:positive, bed\:positive, bathroom\:positive, room service\:negative, TV channels\:negative | |
32. | |
input: The coffee shop has a cozy atmosphere and friendly staff. The coffee was great, and the pastries were fresh. However, the seating was uncomfortable and the place was too noisy. | |
output: atmosphere\:positive, staff\:positive, coffee\:positive, pastries\:positive, seating\:negative, noise\:negative | |
33. | |
input: The museum had a variety of exhibits and was well-maintained. The staff was informative, and the layout was easy to navigate. However, the entrance fee was too high, and the gift shop had limited items. | |
output: exhibits\:positive, maintenance\:positive, staff\:positive, layout\:positive, entrance fee\:negative, gift shop\:negative | |
34. | |
input: The restaurant's ambiance was elegant, and the staff was attentive. The appetizers were great, and the main course was delicious. However, the portions were small, and the dessert was disappointing. | |
output: ambiance\:positive, staff\:positive, appetizers\:positive, main course\:positive, portion size\:negative, dessert\:negative | |
35. | |
input: The online customer support was quick and efficient. The website was easy to navigate, and the checkout process was smooth. However, the product packaging was damaged, and the shipping took longer than expected. | |
output: customer support\:positive, website\:positive, checkout\:positive, packaging\:negative, shipping\:negative | |
36. | |
input: The book was an interesting read, with great character development and a unique storyline. However, the pacing was slow, and the ending was predictable. | |
output: character development\:positive, storyline\:positive, pacing\:negative, ending\:negative | |
37. | |
input: The video game has great graphics and a compelling story. The gameplay mechanics are smooth, and the sound design is immersive. However, there are frequent bugs, and the loading time is long. | |
output: graphics\:positive, story\:positive, gameplay\:positive, sound design\:positive, bugs\:negative, loading time\:negative | |
38. | |
input: The skincare product is gentle on my skin and leaves it feeling smooth. The scent is pleasant | |
38. input: The battery life is amazing and the screen is crystal clear. | |
output: battery life:positive, screen:positive | |
40. input: The receptionist was rude, but the room was clean. | |
output: receptionist:negative, room:positive | |
41. input: This shirt fits well but the fabric feels cheap. | |
output: fit:positive, fabric:negative | |
42. input: The plot of the book was a bit slow but very emotional. | |
output: plot:negative, emotional content:positive | |
43. input: The Wi-Fi in the hotel barely worked and the bathroom was dirty. | |
output: Wi-Fi:negative, bathroom:negative | |
44. input: Their customer support team responded quickly and solved my problem. | |
output: customer support:positive, response time:positive | |
45. input: I like the color and design, but the stitching came loose after one wash. | |
output: color:positive, design:positive, stitching:negative | |
46. input: The professor explains concepts very clearly but gives very tough exams. | |
output: explanations:positive, exams:negative | |
47. input: The application interface is clean and intuitive, but it crashes too often. | |
output: interface:positive, usability:positive, stability:negative | |
48. input: The museum had a good collection, but the lighting was poor. | |
output: collection:positive, lighting:negative | |
49. input: The speaker has good bass but lacks clarity at high volumes. | |
output: bass:positive, clarity:negative | |
50. input: Parking was easy, but getting out took forever due to traffic. | |
output: parking:positive, traffic:negative | |
51. input: The gameplay is addictive but the graphics are outdated. | |
output: gameplay:positive, graphics:negative | |
Now complete the following example- | |
input: """ | |
eos_instruction = ' \noutput:' | |
# Append and prepend the text to the user's input. | |
modified_input = bos_instruction_id + user_input + eos_instruction | |
return modified_input | |
# Assuming you have a function `model_predict` that takes the processed text and returns a prediction. | |
def model_predict(processed_text): | |
# Here you would include the logic to make a prediction based on the processed_text. | |
# This example simply returns the processed text. | |
# tokenize input | |
input_ids = t5_exp.tokenizer(processed_text, return_tensors="pt").input_ids | |
# generate output | |
outputs = t5_exp.model.generate(input_ids, max_length=128) | |
return t5_exp.tokenizer.decode(outputs[0], skip_special_tokens=True) | |
# Create T5 model object along with instructions | |
model_checkpoint = "Homeskills/mt_instruct_absa" | |
t5_exp = T5Generator(model_checkpoint) | |
iface = gr.Interface(fn=lambda x: model_predict(preprocess_input(x)), | |
inputs=[gr.Textbox(label="Input review text here")], | |
outputs=gr.Textbox(label="Extracted aspects & sentiment"), | |
title="Aspect-Based Sentiment Analysis (ABSA)", | |
description="This app will take a sample review segment as input and return the extracted aspects and the corresponding sentiments.", | |
) | |
iface.launch() |