SlouchyBuffalo commited on
Commit
45b358c
Β·
verified Β·
1 Parent(s): a1f9106

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +434 -0
app.py ADDED
@@ -0,0 +1,434 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ from huggingface_hub import InferenceClient
4
+ import json
5
+
6
+ # Initialize the Llama 3.3 model client with Cerebras provider
7
+ import os
8
+
9
+ try:
10
+ client = InferenceClient(
11
+ "meta-llama/Llama-3.3-70B-Instruct",
12
+ provider="cerebras",
13
+ token=os.getenv("HF_TOKEN"),
14
+ )
15
+ except Exception as e:
16
+ print(f"Error initializing Cerebras client: {e}")
17
+ # Fallback to default provider
18
+ client = InferenceClient("meta-llama/Llama-3.3-70B-Instruct")
19
+
20
+ # Turabian-based prompts for each stage
21
+ TURABIAN_PROMPTS = {
22
+ "stage_1": """Based on Turabian's method for developing research arguments, analyze this topic and create a commonsense argument with:
23
+ 1. A clear main CLAIM (what you're arguing)
24
+ 2. 2-3 supporting REASONS (why readers should accept your claim)
25
+ 3. Types of EVIDENCE needed (what would support your reasons)
26
+ 4. Present it as everyday conversation logic
27
+ Make it simple and foundational - this is just the starting point.
28
+ Topic: {input}
29
+ Format your response as:
30
+ CLAIM: [Your main argument]
31
+ REASON 1: [First supporting reason]
32
+ REASON 2: [Second supporting reason]
33
+ REASON 3: [Third reason if needed]
34
+ EVIDENCE TYPES NEEDED: [What kind of evidence you'd need]""",
35
+
36
+ "stage_2": """Help transition this commonsense argument into a research argument framework suitable for academic writing:
37
+ 1. Identify what needs strengthening for a scholarly audience
38
+ 2. Note areas requiring more formal evidence
39
+ 3. Suggest how to frame this as a cooperative conversation with skeptical but receptive colleagues
40
+ 4. Highlight the gap between commonsense and research-level argumentation
41
+ Current Commonsense Argument: {input}
42
+ Format your response with:
43
+ STRENGTHS TO BUILD ON: [What's already strong]
44
+ AREAS NEEDING DEVELOPMENT: [What needs more work]
45
+ ACADEMIC FRAMING: [How to present to scholars]
46
+ NEXT STEPS: [What to focus on in Stage 3]""",
47
+
48
+ "stage_3a": """Transform this working hypothesis into a specific, supportable academic claim:
49
+ 1. Make it an assertion that clearly demands support
50
+ 2. Ensure it's specific and focused (not too broad)
51
+ 3. Check that you can realistically support it with available evidence
52
+ 4. Make sure it goes beyond obvious or trivial statements
53
+ Working Hypothesis: {input}
54
+ Provide:
55
+ REFINED CLAIM: [Your specific, supportable claim]
56
+ WHY THIS WORKS: [Why this claim is effective]
57
+ POTENTIAL CHALLENGES: [What might be difficult to prove]
58
+ SCOPE NOTES: [What you're including/excluding]""",
59
+
60
+ "stage_3b": """Develop comprehensive reasons and evidence strategy for this claim:
61
+ 1. Generate multiple strong supporting reasons
62
+ 2. Identify primary source evidence needed
63
+ 3. Assess evidence quality, quantity, and type requirements
64
+ 4. Note potential challenges readers might raise about your evidence
65
+ Claim: {input}
66
+ Provide:
67
+ REASON 1: [First major reason]
68
+ - Evidence needed: [Specific evidence for this reason]
69
+ REASON 2: [Second major reason]
70
+ - Evidence needed: [Specific evidence for this reason]
71
+ REASON 3: [Third reason if applicable]
72
+ - Evidence needed: [Specific evidence for this reason]
73
+ PRIMARY SOURCES REQUIRED: [What original sources you need]
74
+ EVIDENCE QUALITY STANDARDS: [What makes strong evidence here]
75
+ POTENTIAL EVIDENCE CHALLENGES: [What readers might question]""",
76
+
77
+ "stage_3c": """Anticipate objections and questions from careful, skeptical readers:
78
+ 1. List questions skeptical readers might ask
79
+ 2. Identify internal soundness challenges (evidence quality, reason consistency)
80
+ 3. Identify external soundness challenges (alternative views, exceptions)
81
+ 4. Suggest strong responses to key objections
82
+ Argument: {input}
83
+ Provide:
84
+ READER QUESTIONS: [What will they ask?]
85
+ INTERNAL CHALLENGES: [Questions about evidence/logic]
86
+ EXTERNAL CHALLENGES: [Alternative interpretations/views]
87
+ KEY OBJECTION 1: [Most important objection]
88
+ - Your Response: [How you'll address it]
89
+ KEY OBJECTION 2: [Second most important]
90
+ - Your Response: [How you'll address it]
91
+ ACKNOWLEDGMENT STRATEGY: [How to show you've considered these]""",
92
+
93
+ "stage_3d": """Identify and articulate the warrants (underlying principles) connecting your reasons to your claim:
94
+ 1. Explain the underlying principles/assumptions
95
+ 2. Create clear When-Then statements
96
+ 3. Check if readers will likely accept these warrants
97
+ 4. Note any limiting conditions
98
+ Reason: {reason}
99
+ Claim: {claim}
100
+ Provide:
101
+ UNDERLYING PRINCIPLE: [The basic assumption connecting reason to claim]
102
+ WHEN-THEN STATEMENT: [When X, then Y format]
103
+ WARRANT ACCEPTABILITY: [Will readers accept this principle?]
104
+ POTENTIAL WARRANT CHALLENGES: [What if readers question the principle?]
105
+ LIMITING CONDITIONS: [When might this warrant not apply?]
106
+ MAKING IT EXPLICIT: [How to clearly state this in your argument]""",
107
+
108
+ "stage_4a": """Create a comprehensive storyboard/outline including all argument elements:
109
+ 1. Research question
110
+ 2. Working hypothesis/claim
111
+ 3. Reasons and evidence
112
+ 4. Acknowledgments and responses
113
+ 5. Warrants
114
+ Argument Elements: {input}
115
+ Create:
116
+ RESEARCH QUESTION: [Your driving question]
117
+ MAIN CLAIM: [Your central argument]
118
+ STORYBOARD OUTLINE:
119
+ 1. Introduction
120
+ - Hook/Context
121
+ - Research Question
122
+ - Claim Preview
123
+ 2. Reason 1 + Evidence
124
+ - Warrant
125
+ - Potential Objection + Response
126
+ 3. Reason 2 + Evidence
127
+ - Warrant
128
+ - Potential Objection + Response
129
+ 4. [Additional reasons as needed]
130
+ 5. Conclusion
131
+ - Claim Restatement
132
+ - Broader Implications
133
+ FLOW CHECK: [How sections connect logically]""",
134
+
135
+ "stage_4b": """Organize this argument structure to meet reader needs and expectations:
136
+ 1. Suggest optimal ordering strategies
137
+ 2. Recommend transitional structure
138
+ 3. Consider what academic readers expect
139
+ 4. Plan logical flow between sections
140
+ Argument Elements: {input}
141
+ Provide:
142
+ RECOMMENDED ORGANIZATION: [Best structure approach]
143
+ - Chronological / Topical / Simple-to-Complex / etc.
144
+ SECTION ORDER: [Specific sequence]
145
+ TRANSITIONAL STRATEGY: [How sections will connect]
146
+ READER EXPECTATIONS: [What readers need to see when]
147
+ LOGICAL FLOW NOTES: [Why this order works]
148
+ ALTERNATIVE APPROACHES: [Other viable structures]""",
149
+
150
+ "stage_4c": """Sketch working introduction and conclusion that effectively frame your argument:
151
+ 1. Draft introduction that hooks readers and previews argument
152
+ 2. Draft conclusion that reinforces argument and shows significance
153
+ 3. Ensure they work together as a frame
154
+ Main Argument: {input}
155
+ Create:
156
+ INTRODUCTION DRAFT:
157
+ - Opening Hook: [Engaging start]
158
+ - Context/Background: [Essential background]
159
+ - Research Question: [Your driving question]
160
+ - Claim Statement: [Clear claim]
161
+ - Roadmap: [What's coming]
162
+ CONCLUSION DRAFT:
163
+ - Claim Restatement: [Reaffirm your argument]
164
+ - Key Evidence Summary: [Main support]
165
+ - Broader Implications: [Why it matters]
166
+ - Future Research: [What's next]
167
+ FRAME COHERENCE: [How intro and conclusion work together]""",
168
+
169
+ "stage_4d": """Identify and define key terms that will provide conceptual coherence throughout your paper:
170
+ 1. Select terms representing core concepts
171
+ 2. Ensure terms will run consistently through the paper
172
+ 3. Check for conceptual clarity and consistency
173
+ Argument: {input}
174
+ Provide:
175
+ KEY TERMS TO DEFINE:
176
+ 1. [Term 1]: [Definition and importance]
177
+ 2. [Term 2]: [Definition and importance]
178
+ 3. [Term 3]: [Definition and importance]
179
+ [Continue as needed]
180
+ CONCEPTUAL CONSISTENCY: [How terms relate to each other]
181
+ DEFINITION STRATEGY: [Where/how to define terms]
182
+ USAGE GUIDELINES: [How to use terms consistently]
183
+ POTENTIAL CONFUSION: [Terms readers might misunderstand]""",
184
+
185
+ "stage_5": """Provide comprehensive drafting guidance and best practices:
186
+ 1. Effective writing habits
187
+ 2. Source integration techniques
188
+ 3. Evidence relevance demonstration
189
+ 4. Using headings and key terms effectively
190
+ Writing Plan: {input}
191
+ Guidance:
192
+ WRITING HABITS:
193
+ - Daily writing schedule suggestions
194
+ - Productivity techniques
195
+ - Dealing with writer's block
196
+ SOURCE INTEGRATION:
197
+ - When to quote vs. paraphrase vs. summarize
198
+ - How to introduce sources effectively
199
+ - Attribution best practices
200
+ EVIDENCE CONNECTION:
201
+ - Making evidence relevance clear
202
+ - Linking evidence to reasons and claims
203
+ - Handling complex or detailed evidence
204
+ STRUCTURE MAINTENANCE:
205
+ - Using headings effectively
206
+ - Tracking key terms
207
+ - Maintaining argument thread
208
+ DRAFTING TIPS: [Practical writing advice]""",
209
+
210
+ "stage_6": """Create a comprehensive revision checklist based on Turabian's standards:
211
+ 1. Check for argument blind spots
212
+ 2. Review introduction, conclusion, and claim clarity
213
+ 3. Assess overall body coherence
214
+ 4. Provide paragraph-level revision guidance
215
+ Draft Description: {input}
216
+ Revision Checklist:
217
+ ARGUMENT STRENGTH:
218
+ β–‘ Blind spots check (unconsidered counterarguments?)
219
+ β–‘ Alternative interpretations addressed?
220
+ β–‘ Evidence gaps filled?
221
+ STRUCTURAL CLARITY:
222
+ β–‘ Introduction clearly frames argument?
223
+ β–‘ Conclusion reinforces without repeating?
224
+ β–‘ Claim visible and specific?
225
+ BODY COHERENCE:
226
+ β–‘ Key terms used consistently?
227
+ β–‘ Section breaks logical?
228
+ β–‘ Each section contributes to main argument?
229
+ β–‘ Transitions smooth and clear?
230
+ PARAGRAPH LEVEL:
231
+ β–‘ Each paragraph has clear purpose?
232
+ β–‘ Evidence clearly supports reasons?
233
+ β–‘ Warrants made explicit where needed?
234
+ REVISION PRIORITIES: [What to focus on first]"""
235
+ }
236
+
237
+ # Track user progress through stages
238
+ def get_stage_info(stage_key):
239
+ stage_info = {
240
+ "stage_1": {"name": "Commonsense Argument", "description": "Create your foundational argument"},
241
+ "stage_2": {"name": "Research Transition", "description": "Move from commonsense to academic"},
242
+ "stage_3a": {"name": "Claim Development", "description": "Refine your specific claim"},
243
+ "stage_3b": {"name": "Evidence Mapping", "description": "Plan reasons and evidence"},
244
+ "stage_3c": {"name": "Objection Analysis", "description": "Anticipate reader challenges"},
245
+ "stage_3d": {"name": "Warrant Identification", "description": "Connect reasons to claims"},
246
+ "stage_4a": {"name": "Storyboard Creation", "description": "Outline your argument"},
247
+ "stage_4b": {"name": "Organization Planning", "description": "Structure for readers"},
248
+ "stage_4c": {"name": "Introduction & Conclusion", "description": "Frame your argument"},
249
+ "stage_4d": {"name": "Key Terms", "description": "Define core concepts"},
250
+ "stage_5": {"name": "Drafting Guidance", "description": "Write your first draft"},
251
+ "stage_6": {"name": "Revision Checklist", "description": "Polish your argument"}
252
+ }
253
+ return stage_info.get(stage_key, {"name": "Unknown", "description": ""})
254
+
255
+ # Main processing function
256
+ @spaces.GPU
257
+ def process_stage(stage, input_text, reason_text="", claim_text=""):
258
+ """Process input through selected Turabian stage"""
259
+
260
+ if not input_text.strip():
261
+ return "Please provide input text to analyze."
262
+
263
+ # Get the appropriate prompt - fixed the key mapping
264
+ prompt_template = TURABIAN_PROMPTS.get(stage)
265
+
266
+ if not prompt_template:
267
+ return f"Error: Stage {stage} not found."
268
+
269
+ # Handle special case for warrant stage that needs both reason and claim
270
+ if stage == "stage_3d":
271
+ if not reason_text.strip() or not claim_text.strip():
272
+ return "For warrant analysis, please provide both a reason and a claim."
273
+ prompt = prompt_template.format(reason=reason_text, claim=claim_text)
274
+ else:
275
+ prompt = prompt_template.format(input=input_text)
276
+
277
+ try:
278
+ # Generate response using Llama 3.3
279
+ messages = [{"role": "user", "content": prompt}]
280
+
281
+ response = client.chat_completion(
282
+ messages=messages,
283
+ max_tokens=1500,
284
+ temperature=0.7,
285
+ top_p=0.9
286
+ )
287
+
288
+ return response.choices[0].message.content
289
+
290
+ except Exception as e:
291
+ return f"Error processing request: {str(e)}"
292
+
293
+ # Custom CSS for better styling
294
+ custom_css = """
295
+ .stage-container {
296
+ border: 2px solid #4CAF50;
297
+ border-radius: 10px;
298
+ padding: 20px;
299
+ margin: 10px 0;
300
+ background-color: #f9f9f9;
301
+ }
302
+ .stage-title {
303
+ color: #2E7D32;
304
+ font-size: 1.2em;
305
+ font-weight: bold;
306
+ margin-bottom: 10px;
307
+ }
308
+ .progress-indicator {
309
+ background-color: #E3F2FD;
310
+ padding: 10px;
311
+ border-radius: 5px;
312
+ margin-bottom: 15px;
313
+ }
314
+ .output-box {
315
+ background-color: #FAFAFA;
316
+ border: 1px solid #E0E0E0;
317
+ border-radius: 5px;
318
+ padding: 15px;
319
+ font-family: monospace;
320
+ line-height: 1.6;
321
+ }
322
+ """
323
+
324
+ # Main Gradio interface
325
+ with gr.Blocks(title="Turabian Research Argument Assistant", css=custom_css, theme=gr.themes.Soft()) as app:
326
+ gr.Markdown("""
327
+ # πŸ“š Turabian Research Argument Assistant
328
+ ### *Based on "A Manual for Writers of Research Papers, Theses, and Dissertations" by Kate L. Turabian*
329
+
330
+ Develop your research argument step-by-step using Turabian's proven methodology. This tool guides you through all 6 stages from commonsense argument to final revision.
331
+ """)
332
+
333
+ with gr.Row():
334
+ with gr.Column(scale=2):
335
+ gr.Markdown("## 🎯 Select Your Stage")
336
+
337
+ stage = gr.Radio(
338
+ choices=[
339
+ ("πŸ“ Stage 1: Commonsense Argument", "stage_1"),
340
+ ("πŸ”„ Stage 2: Research Transition", "stage_2"),
341
+ ("πŸ—οΈ Stage 3A: Claim Development", "stage_3a"),
342
+ ("πŸ“Š Stage 3B: Evidence Mapping", "stage_3b"),
343
+ ("❓ Stage 3C: Objection Analysis", "stage_3c"),
344
+ ("πŸ”— Stage 3D: Warrant Identification", "stage_3d"),
345
+ ("πŸ“‹ Stage 4A: Storyboard Creation", "stage_4a"),
346
+ ("πŸ“ Stage 4B: Organization Planning", "stage_4b"),
347
+ ("✍️ Stage 4C: Introduction & Conclusion", "stage_4c"),
348
+ ("πŸ”‘ Stage 4D: Key Terms", "stage_4d"),
349
+ ("πŸ“ Stage 5: Drafting Guidance", "stage_5"),
350
+ ("πŸ” Stage 6: Revision Checklist", "stage_6")
351
+ ],
352
+ value="stage_1",
353
+ label="Choose Your Current Stage"
354
+ )
355
+
356
+ # Dynamic stage information
357
+ stage_info = gr.Markdown("**Stage 1: Commonsense Argument** - Create your foundational argument")
358
+
359
+ # Input section
360
+ gr.Markdown("## πŸ“ Your Input")
361
+ input_text = gr.Textbox(
362
+ label="Main Input",
363
+ placeholder="Enter your topic, argument, or text for this stage...",
364
+ lines=8,
365
+ max_lines=15
366
+ )
367
+
368
+ # Special inputs for warrant stage
369
+ with gr.Row(visible=False) as warrant_inputs:
370
+ reason_text = gr.Textbox(
371
+ label="Reason",
372
+ placeholder="Enter the specific reason...",
373
+ lines=3
374
+ )
375
+ claim_text = gr.Textbox(
376
+ label="Claim",
377
+ placeholder="Enter the specific claim...",
378
+ lines=3
379
+ )
380
+
381
+ process_btn = gr.Button("πŸš€ Process Stage", variant="primary", size="lg")
382
+
383
+ with gr.Column(scale=3):
384
+ gr.Markdown("## πŸ“‹ Stage Output")
385
+ output = gr.Textbox(
386
+ label="Analysis Results",
387
+ lines=30,
388
+ max_lines=40,
389
+ show_copy_button=True,
390
+ elem_classes=["output-box"]
391
+ )
392
+
393
+ gr.Markdown("""
394
+ ## πŸ“– How to Use This Tool
395
+
396
+ 1. **Start with Stage 1** - Input your basic topic/idea
397
+ 2. **Progress sequentially** - Each stage builds on the previous
398
+ 3. **Copy outputs** - Use results from one stage as input for the next
399
+ 4. **Take your time** - Turabian's method requires careful development
400
+
401
+ ## πŸ’‘ Tips
402
+ - Keep outputs from each stage for reference
403
+ - Don't skip stages - each builds essential elements
404
+ - Stage 3D (Warrants) requires both a reason and claim
405
+ - Use stage 4+ outputs to guide your actual writing
406
+ """)
407
+
408
+ # Update stage information when selection changes
409
+ def update_stage_info(stage_key):
410
+ info = get_stage_info(stage_key)
411
+ stage_desc = f"**{info['name']}** - {info['description']}"
412
+
413
+ # Show/hide warrant special inputs
414
+ if stage_key == "stage_3d":
415
+ return stage_desc, gr.update(visible=True)
416
+ else:
417
+ return stage_desc, gr.update(visible=False)
418
+
419
+ stage.change(
420
+ fn=update_stage_info,
421
+ inputs=[stage],
422
+ outputs=[stage_info, warrant_inputs]
423
+ )
424
+
425
+ # Process button click
426
+ process_btn.click(
427
+ fn=process_stage,
428
+ inputs=[stage, input_text, reason_text, claim_text],
429
+ outputs=output,
430
+ show_progress=True
431
+ )
432
+
433
+ if __name__ == "__main__":
434
+ app.launch()