Spaces:
Sleeping

CritLit / app.py
DeftDaedalus's picture
Update app.py
18ac739 verified
Raw History Blame Contribute Delete
5.08 kB
import gradio as gr
from groq import Groq
from tavily import TavilyClient
import os
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
tavily_client = TavilyClient(api_key=os.environ.get("TAVILY_API_KEY"))
def critique_writing(text, genre, persona):
critique_prompt = f"""
You are a world-class writing tutor critiquing a piece of {genre} writing.
{f'Critique in the style and perspective of {persona}.' if persona else ''}
Analyze the following writing and provide detailed feedback on:
1. Structure and organization
2. Clarity and coherence
3. Style and voice
4. Argument strength (if applicable)
5. Specific suggestions for improvement
Writing:
{text}
Return your critique in clear sections.
"""
critique = groq_client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[{"role": "user", "content": critique_prompt}],
max_tokens=1500
).choices[0].message.content
search_query = f"excellent examples of {genre} writing techniques"
results = tavily_client.search(search_query, max_results=2)
references = "\n".join([f"- [{r['title']}]({r['url']})" for r in results.get('results', [])])
output = f"## Writing Critique\n\n{critique}"
if references:
output += f"\n\n## Reference Examples\n{references}"
return output
def generate_prompt(themes, genre, length):
themes_str = ", ".join(themes) if themes else "general"
prompt = f"""
You are a creative writing coach. Generate an original and compelling writing prompt.
Themes to incorporate: {themes_str}
Genre: {genre}
Desired story length: {length}
The prompt should:
- Be specific enough to inspire but open enough to allow creativity
- Include an interesting character situation or conflict
- Hint at deeper thematic questions
- Feel fresh and non-generic
Return just the prompt, no preamble.
"""
result = groq_client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[{"role": "user", "content": prompt}],
max_tokens=500
).choices[0].message.content
return result
with gr.Blocks(title="CritLit - AI Writing Tutor") as demo:
gr.Markdown("# ✍️ CritLit\n### AI-powered writing critique and prompt generator")
with gr.Tabs():
with gr.Tab("📝 Critique My Writing"):
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="Paste your writing here",
placeholder="Enter your essay, story, argument, or any writing...",
lines=15
)
genre = gr.Dropdown(
choices=["essay", "short story", "argument", "poem", "academic paper", "blog post"],
label="Writing Genre",
value="essay"
)
persona = gr.Textbox(
label="Critique Persona (optional)",
placeholder="e.g. Stephen King, a Cambridge examiner, Hemingway..."
)
submit_btn = gr.Button("Critique My Writing", variant="primary")
with gr.Column():
critique_output = gr.Markdown(label="Critique")
submit_btn.click(
fn=critique_writing,
inputs=[text_input, genre, persona],
outputs=critique_output
)
with gr.Tab("💡 Generate Writing Prompt"):
with gr.Row():
with gr.Column():
themes = gr.CheckboxGroup(
choices=[
"homicide", "philosophy", "toska", "love",
"betrayal", "identity", "power", "grief",
"redemption", "isolation", "memory", "technology",
"religion", "war", "madness", "time"
],
label="Select Themes",
value=["philosophy"]
)
prompt_genre = gr.Dropdown(
choices=["short story", "novel chapter", "flash fiction", "screenplay", "poem"],
label="Genre",
value="short story"
)
length = gr.Radio(
choices=["flash (under 1000 words)", "short (1000-5000 words)", "long (5000+ words)"],
label="Desired Length",
value="short (1000-5000 words)"
)
prompt_btn = gr.Button("Generate Prompt", variant="primary")
with gr.Column():
prompt_output = gr.Textbox(label="Your Writing Prompt", lines=10)
prompt_btn.click(
fn=generate_prompt,
inputs=[themes, prompt_genre, length],
outputs=prompt_output
)
demo.launch()