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