# Writing_tab.py
# Description: This file contains the functions that are used for writing in the Gradio UI.
#
# Imports
#
# External Imports
import logging
import gradio as gr
import textstat
#
# Local Imports
from App_Function_Libraries.Summarization.Summarization_General_Lib import perform_summarization
from App_Function_Libraries.Utils.Utils import default_api_endpoint, global_api_endpoints, format_api_name
#
########################################################################################################################
#
# 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", visible=True):
try:
default_value = None
if default_api_endpoint:
if default_api_endpoint in global_api_endpoints:
default_value = format_api_name(default_api_endpoint)
else:
logging.warning(f"Default API endpoint '{default_api_endpoint}' not found in global_api_endpoints")
except Exception as e:
logging.error(f"Error setting default API endpoint: {str(e)}")
default_value = None
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="""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.[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]
)
# Refactored API selection dropdown
api_name_input = gr.Dropdown(
choices=["None"] + [format_api_name(api) for api in global_api_endpoints],
value=default_value,
label="API for Analysis (Optional)"
)
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", visible=True):
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 = "
Original Text: {entry['text'][:100]}...
" feedback = entry.get('feedback') if feedback: html += f"Feedback: {feedback[:200]}...
" else: html += "Feedback: No feedback provided.
" html += "