Spaces:
Runtime error
Runtime error
| from difflib import Differ | |
| import gradio as gr | |
| from transformers import pipeline | |
| pipe = pipeline("summarization", "dominguesm/positive-reframing-en") | |
| def predict(text, operation): | |
| try: | |
| res = pipe(f"[{operation}]: {text}", max_length=124) | |
| except Exception as e: | |
| return e | |
| d = Differ() | |
| return ( | |
| res[0]["summary_text"], | |
| [ | |
| (token[2:], token[0] if token[0] != " " else None) | |
| for token in d.compare(text, res[0]["summary_text"]) | |
| ], | |
| ) | |
| # return res[0]["summary_text"] | |
| iface = gr.Interface( | |
| fn=predict, | |
| inputs=[ | |
| gr.Textbox( | |
| lines=1, | |
| placeholder=( | |
| f"It is hard to take time when in a hurry, but you could do what you want or what is in your nature." | |
| ), | |
| ), | |
| gr.Radio( | |
| [ | |
| "growth", | |
| "impermanence", | |
| "neutralizing", | |
| "optimism", | |
| "self_affirmation", | |
| "thankfulness", | |
| ] | |
| ), | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="Generated Text"), | |
| gr.HighlightedText( | |
| label="Diff", | |
| combine_adjacent=True, | |
| ).style(color_map={"+": "green", "-": "red"}), | |
| ], | |
| examples=[ | |
| [ | |
| "You know I really don't care about the power struggle between the papacy and secular authority in the medieval ages. stupid", | |
| "growth", | |
| ], | |
| [ | |
| "thinking about my future makes me want to go live on a island alone forever. annoyed", | |
| "optimism", | |
| ], | |
| [ | |
| "Who would have ever guessed that it would be so freaking hard to get three different grades from two different schools together.", | |
| "thankfulness", | |
| ], | |
| ], | |
| ) | |
| iface.launch() | |
| import streamlit as st | |
| # Dataset | |
| examples = [ | |
| [ | |
| "The power struggle between the papacy and secular authority in the medieval ages is as unimportant to me as a drop of water in the ocean. (simile)", | |
| "growth", | |
| ], | |
| [ | |
| "Contemplating my future feels like being engulfed by the urge to escape to a secluded island, forever. (metaphor)", | |
| "optimism", | |
| ], | |
| [ | |
| "Who would have thought that uniting three different grades from two different schools would be as tough as nailing jelly to a wall? (simile)", | |
| "thankfulness", | |
| ], | |
| [ | |
| "Her laughter was like the tinkling of silver bells, filling the room with joy. (simile)", | |
| "happiness", | |
| ], | |
| [ | |
| "The thunder roared and boomed, striking fear in the hearts of those who heard it. (onomatopoeia)", | |
| "courage", | |
| ], | |
| ] | |
| language_features = [ | |
| "Metaphor", | |
| "Simile", | |
| "Onomatopoeia", | |
| "Alliteration", | |
| "Assonance", | |
| "Hyperbole", | |
| "Personification", | |
| "Oxymoron", | |
| "Paradox", | |
| "Pun", | |
| "Irony", | |
| "Sarcasm", | |
| "Allusion", | |
| "Imagery", | |
| "Symbolism", | |
| "Anaphora", | |
| "Epistrophe", | |
| "Parallelism", | |
| "Euphemism", | |
| "Synecdoche", | |
| ] | |
| # Streamlit app | |
| st.title("Language Feature Emoji Reference") | |
| # Table of buttons | |
| table_data = [language_features[i:i + 3] for i in range(0, len(language_features), 3)] | |
| for row in table_data: | |
| row_buttons = st.beta_columns(len(row)) | |
| for i, feature in enumerate(row): | |
| if row_buttons[i].button(feature): | |
| for example in examples: | |
| if feature.lower() in example[0]: | |
| st.write(f"**{feature}:** {example[0]}") | |
| st.write(f"**Emoji:** {example[1]}") | |
| st.write("---") | |