Spaces:
Sleeping
Sleeping
File size: 18,381 Bytes
41ba402 4e1f4a3 41ba402 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# Writing_tab.py
# Description: This file contains the functions that are used for writing in the Gradio UI.
#
# Imports
#
# External Imports
import gradio as gr
import textstat
#
# Local Imports
from App_Function_Libraries.Summarization.Summarization_General_Lib import perform_summarization
#
########################################################################################################################
#
# Functions:
def adjust_tone(text, concise, casual, api_name, api_key):
tones = [
{"tone": "concise", "weight": concise},
{"tone": "casual", "weight": casual},
{"tone": "professional", "weight": 1 - casual},
{"tone": "expanded", "weight": 1 - concise}
]
tones = sorted(tones, key=lambda x: x['weight'], reverse=True)[:2]
tone_prompt = " and ".join([f"{t['tone']} (weight: {t['weight']:.2f})" for t in tones])
prompt = f"Rewrite the following text to match these tones: {tone_prompt}. Text: {text}"
# Performing tone adjustment request...
adjusted_text = perform_summarization(api_name, text, prompt, api_key)
return adjusted_text
def grammar_style_check(input_text, custom_prompt, api_name, api_key, system_prompt):
default_prompt = "Please analyze the following text for grammar and style. Offer suggestions for improvement and point out any misused words or incorrect spellings:\n\n"
full_prompt = custom_prompt if custom_prompt else default_prompt
full_text = full_prompt + input_text
return perform_summarization(api_name, full_text, custom_prompt, api_key, system_prompt)
def create_grammar_style_check_tab():
with gr.TabItem("Grammar and Style Check"):
with gr.Row():
with gr.Column():
gr.Markdown("# Grammar and Style Check")
gr.Markdown("This utility checks the grammar and style of the provided text by feeding it to an LLM and returning suggestions for improvement.")
input_text = gr.Textbox(label="Input Text", lines=10)
custom_prompt_checkbox = gr.Checkbox(label="Use Custom Prompt", value=False, visible=True)
system_prompt_input = gr.Textbox(label="System Prompt", placeholder="Please analyze the provided text for grammar and style. Offer any suggestions or points to improve you can identify. Additionally please point out any misuses of any words or incorrect spellings.", lines=5, visible=False)
custom_prompt_input = gr.Textbox(label="user Prompt",
value="""<s>You are a bulleted notes specialist. [INST]```When creating comprehensive bulleted notes, you should follow these guidelines: Use multiple headings based on the referenced topics, not categories like quotes or terms. Headings should be surrounded by bold formatting and not be listed as bullet points themselves. Leave no space between headings and their corresponding list items underneath. Important terms within the content should be emphasized by setting them in bold font. Any text that ends with a colon should also be bolded. Before submitting your response, review the instructions, and make any corrections necessary to adhered to the specified format. Do not reference these instructions within the notes.``` \nBased on the content between backticks create comprehensive bulleted notes.[/INST]
**Bulleted Note Creation Guidelines**
**Headings**:
- Based on referenced topics, not categories like quotes or terms
- Surrounded by **bold** formatting
- Not listed as bullet points
- No space between headings and list items underneath
**Emphasis**:
- **Important terms** set in bold font
- **Text ending in a colon**: also bolded
**Review**:
- Ensure adherence to specified format
- Do not reference these instructions in your response.</s>[INST] {{ .Prompt }} [/INST]
""",
lines=3,
visible=False)
custom_prompt_checkbox.change(
fn=lambda x: (gr.update(visible=x), gr.update(visible=x)),
inputs=[custom_prompt_checkbox],
outputs=[custom_prompt_input, system_prompt_input]
)
api_name_input = gr.Dropdown(
choices=[None, "Local-LLM", "OpenAI", "Anthropic", "Cohere", "Groq", "DeepSeek", "Mistral", "OpenRouter",
"Llama.cpp", "Kobold", "Ooba", "Tabbyapi", "VLLM","ollama", "HuggingFace", "Custom-OpenAI-API"],
value=None,
label="API for Grammar Check"
)
api_key_input = gr.Textbox(label="API Key (if not set in Config_Files/config.txt)", placeholder="Enter your API key here",
type="password")
check_grammar_button = gr.Button("Check Grammar and Style")
with gr.Column():
gr.Markdown("# Resulting Suggestions")
gr.Markdown("(Keep in mind the API used can affect the quality of the suggestions)")
output_text = gr.Textbox(label="Grammar and Style Suggestions", lines=15)
check_grammar_button.click(
fn=grammar_style_check,
inputs=[input_text, custom_prompt_input, api_name_input, api_key_input, system_prompt_input],
outputs=output_text
)
def create_tone_adjustment_tab():
with gr.TabItem("Tone Analyzer & Editor"):
with gr.Row():
with gr.Column():
input_text = gr.Textbox(label="Input Text", lines=10)
concise_slider = gr.Slider(minimum=0, maximum=1, value=0.5, label="Concise vs Expanded")
casual_slider = gr.Slider(minimum=0, maximum=1, value=0.5, label="Casual vs Professional")
api_name_input = gr.Dropdown(
choices=[None, "Local-LLM", "OpenAI", "Anthropic", "Cohere", "Groq", "DeepSeek", "Mistral", "OpenRouter",
"Llama.cpp", "Kobold", "Ooba", "Tabbyapi", "VLLM","ollama", "HuggingFace", "Custom-OpenAI-API"],
value=None,
label="API for Grammar Check"
)
api_key_input = gr.Textbox(label="API Key (if not set in Config_Files/config.txt)", placeholder="Enter your API key here",
type="password")
adjust_btn = gr.Button("Adjust Tone")
with gr.Column():
output_text = gr.Textbox(label="Adjusted Text", lines=15)
adjust_btn.click(
adjust_tone,
inputs=[input_text, concise_slider, casual_slider],
outputs=output_text
)
persona_prompts = {
"Hemingway": "As Ernest Hemingway, known for concise and straightforward prose, provide feedback on the following text:",
"Shakespeare": "Channel William Shakespeare's poetic style and provide feedback on the following text:",
"Jane Austen": "Embodying Jane Austen's wit and social commentary, critique the following text:",
"Stephen King": "With Stephen King's flair for suspense and horror, analyze the following text:",
"J.K. Rowling": "As J.K. Rowling, creator of the magical world of Harry Potter, review the following text:"
}
def generate_writing_feedback(text, persona, aspect, api_name, api_key):
if isinstance(persona, dict): # If it's a character card
base_prompt = f"You are {persona['name']}. {persona['personality']}\n\nScenario: {persona['scenario']}\n\nRespond to the following message in character:"
else: # If it's a regular persona
base_prompt = persona_prompts.get(persona, f"As {persona}, provide feedback on the following text:")
if aspect != "Overall":
prompt = f"{base_prompt}\n\nFocus specifically on the {aspect.lower()} in the following text:\n\n{text}"
else:
prompt = f"{base_prompt}\n\n{text}"
return perform_summarization(api_name, text, prompt, api_key, system_message="You are a helpful AI assistant. You will respond to the user as if you were the persona declared in the user prompt.")
def generate_writing_prompt(persona, api_name, api_key):
prompt = f"Generate a writing prompt in the style of {persona}. The prompt should inspire a short story or scene that reflects {persona}'s typical themes and writing style."
#FIXME
return perform_summarization(api_name, prompt, "", api_key, system_message="You are a helpful AI assistant. You will respond to the user as if you were the persona declared in the user prompt." )
def calculate_readability(text):
ease = textstat.flesch_reading_ease(text)
grade = textstat.flesch_kincaid_grade(text)
return f"Readability: Flesch Reading Ease: {ease:.2f}, Flesch-Kincaid Grade Level: {grade:.2f}"
def generate_feedback_history_html(history):
html = "<h3>Recent Feedback History</h3>"
for entry in reversed(history):
html += f"<details><summary>{entry['persona']} Feedback</summary>"
html += f"<p><strong>Original Text:</strong> {entry['text'][:100]}...</p>"
feedback = entry.get('feedback')
if feedback:
html += f"<p><strong>Feedback:</strong> {feedback[:200]}...</p>"
else:
html += "<p><strong>Feedback:</strong> No feedback provided.</p>"
html += "</details>"
return html
# FIXME
def create_document_feedback_tab():
with gr.TabItem("Writing Feedback"):
with gr.Row():
with gr.Column(scale=2):
input_text = gr.Textbox(label="Your Writing", lines=10)
persona_dropdown = gr.Dropdown(
label="Select Persona",
choices=[
"Agatha Christie",
"Arthur Conan Doyle",
"Charles Bukowski",
"Charles Dickens",
"Chinua Achebe",
"Cormac McCarthy",
"David Foster Wallace",
"Edgar Allan Poe",
"F. Scott Fitzgerald",
"Flannery O'Connor",
"Franz Kafka",
"Fyodor Dostoevsky",
"Gabriel Garcia Marquez",
"George R.R. Martin",
"George Orwell",
"Haruki Murakami",
"Hemingway",
"Herman Melville",
"Isabel Allende",
"James Joyce",
"Jane Austen",
"J.K. Rowling",
"J.R.R. Tolkien",
"Jorge Luis Borges",
"Kurt Vonnegut",
"Leo Tolstoy",
"Margaret Atwood",
"Mark Twain",
"Mary Shelley",
"Milan Kundera",
"Naguib Mahfouz",
"Neil Gaiman",
"Octavia Butler",
"Philip K Dick",
"Ray Bradbury",
"Salman Rushdie",
"Shakespeare",
"Stephen King",
"Toni Morrison",
"T.S. Eliot",
"Ursula K. Le Guin",
"Virginia Woolf",
"Virginia Woolf",
"Zadie Smith"],
value="Hemingway"
)
custom_persona_name = gr.Textbox(label="Custom Persona Name")
custom_persona_description = gr.Textbox(label="Custom Persona Description", lines=3)
add_custom_persona_button = gr.Button("Add Custom Persona")
aspect_dropdown = gr.Dropdown(
label="Focus Feedback On",
choices=["Overall", "Grammar", "Word choice", "Structure of delivery", "Character Development", "Character Dialogue", "Descriptive Language", "Plot Structure"],
value="Overall"
)
api_name_input = gr.Dropdown(
choices=[None, "Local-LLM", "OpenAI", "Anthropic", "Cohere", "Groq", "DeepSeek", "Mistral", "OpenRouter",
"Llama.cpp", "Kobold", "Ooba", "Tabbyapi", "VLLM", "ollama", "HuggingFace", "Custom-OpenAI-API"],
value=None,
label="API for Feedback"
)
api_key_input = gr.Textbox(label="API Key (if not set in Config_Files/config.txt)", type="password")
get_feedback_button = gr.Button("Get Feedback")
generate_prompt_button = gr.Button("Generate Writing Prompt")
with gr.Column(scale=2):
feedback_output = gr.Textbox(label="Feedback", lines=15)
readability_output = gr.Textbox(label="Readability Metrics")
feedback_history_display = gr.HTML(label="Feedback History")
with gr.Row():
compare_personas = gr.CheckboxGroup(
choices=[
"Agatha Christie",
"Arthur Conan Doyle",
"Charles Bukowski",
"Charles Dickens",
"Chinua Achebe",
"Cormac McCarthy",
"David Foster Wallace",
"Edgar Allan Poe",
"F. Scott Fitzgerald",
"Flannery O'Connor",
"Franz Kafka",
"Fyodor Dostoevsky",
"Gabriel Garcia Marquez",
"George R.R. Martin",
"George Orwell",
"Haruki Murakami",
"Hemingway",
"Herman Melville",
"Isabel Allende",
"James Joyce",
"Jane Austen",
"J.K. Rowling",
"J.R.R. Tolkien",
"Jorge Luis Borges",
"Kurt Vonnegut",
"Leo Tolstoy",
"Margaret Atwood",
"Mark Twain",
"Mary Shelley",
"Milan Kundera",
"Naguib Mahfouz",
"Neil Gaiman",
"Octavia Butler",
"Philip K Dick",
"Ray Bradbury",
"Salman Rushdie",
"Shakespeare",
"Stephen King",
"Toni Morrison",
"T.S. Eliot",
"Ursula K. Le Guin",
"Virginia Woolf",
"Virginia Woolf",
"Zadie Smith"],
label="Compare Multiple Persona's Feedback at Once(Compares existing feedback, doesn't create new ones)"
)
with gr.Row():
compare_button = gr.Button("Compare Feedback")
feedback_history = gr.State([])
def add_custom_persona(name, description):
updated_choices = persona_dropdown.choices + [name]
persona_prompts[name] = f"As {name}, {description}, provide feedback on the following text:"
return gr.update(choices=updated_choices)
def update_feedback_history(current_text, persona, feedback):
# Ensure feedback_history.value is initialized and is a list
if feedback_history.value is None:
feedback_history.value = []
history = feedback_history.value
# Append the new entry to the history
history.append({"text": current_text, "persona": persona, "feedback": feedback})
# Keep only the last 5 entries in the history
feedback_history.value = history[-10:]
# Generate and return the updated HTML
return generate_feedback_history_html(feedback_history.value)
def compare_feedback(text, selected_personas, api_name, api_key):
results = []
for persona in selected_personas:
feedback = generate_writing_feedback(text, persona, "Overall", api_name, api_key)
results.append(f"### {persona}'s Feedback:\n{feedback}\n\n")
return "\n".join(results)
add_custom_persona_button.click(
fn=add_custom_persona,
inputs=[custom_persona_name, custom_persona_description],
outputs=persona_dropdown
)
get_feedback_button.click(
fn=lambda text, persona, aspect, api_name, api_key: (
generate_writing_feedback(text, persona, aspect, api_name, api_key),
calculate_readability(text),
update_feedback_history(text, persona, generate_writing_feedback(text, persona, aspect, api_name, api_key))
),
inputs=[input_text, persona_dropdown, aspect_dropdown, api_name_input, api_key_input],
outputs=[feedback_output, readability_output, feedback_history_display]
)
compare_button.click(
fn=compare_feedback,
inputs=[input_text, compare_personas, api_name_input, api_key_input],
outputs=feedback_output
)
generate_prompt_button.click(
fn=generate_writing_prompt,
inputs=[persona_dropdown, api_name_input, api_key_input],
outputs=input_text
)
return input_text, feedback_output, readability_output, feedback_history_display
def create_creative_writing_tab():
with gr.TabItem("Creative Writing Assistant"):
gr.Markdown("# Utility to be added...")
def create_mikupad_tab():
with gr.TabItem("Mikupad"):
gr.Markdown("I Wish. Gradio won't embed it successfully...")
#
# End of Writing_tab.py
########################################################################################################################
|