Spaces:
Sleeping

File size: 5,083 Bytes
10f0ab7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18ac739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10f0ab7
18ac739
10f0ab7
18ac739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10f0ab7
 
18ac739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10f0ab7
 
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
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()