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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
from utils.retriever import KnowledgeRetriever
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
class AccessibilityChatbot:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
# Initialize DeepSeek-R1 client
|
| 10 |
+
self.client = InferenceClient(
|
| 11 |
+
model="deepseek-ai/DeepSeek-R1",
|
| 12 |
+
token=os.getenv("HF_TOKEN")
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Initialize knowledge retriever
|
| 16 |
+
self.retriever = KnowledgeRetriever()
|
| 17 |
+
|
| 18 |
+
# System prompt for accessibility education
|
| 19 |
+
self.system_prompt = """You are an expert web accessibility instructor helping university students learn about web accessibility.
|
| 20 |
+
|
| 21 |
+
Your knowledge comes from WebAIM resources, which are authoritative sources for web accessibility information.
|
| 22 |
+
|
| 23 |
+
Guidelines for responses:
|
| 24 |
+
1. Provide clear, student-friendly explanations
|
| 25 |
+
2. Use the provided WebAIM context to answer questions accurately
|
| 26 |
+
3. Always cite your sources by mentioning the WebAIM document and page number
|
| 27 |
+
4. Include practical examples and code snippets when relevant
|
| 28 |
+
5. Break down complex concepts into digestible parts
|
| 29 |
+
6. Encourage best practices and standards compliance
|
| 30 |
+
7. If asked about assignments, provide actionable guidance
|
| 31 |
+
|
| 32 |
+
Remember: You're teaching students, so be encouraging and educational while maintaining accuracy."""
|
| 33 |
+
|
| 34 |
+
def generate_response(self, message, history):
|
| 35 |
+
"""Generate response using DeepSeek-R1 with WebAIM context"""
|
| 36 |
+
|
| 37 |
+
# Retrieve relevant content from WebAIM PDFs
|
| 38 |
+
relevant_content = self.retriever.retrieve_relevant_content(message)
|
| 39 |
+
context = self.retriever.format_context_for_llm(relevant_content)
|
| 40 |
+
|
| 41 |
+
# Prepare messages for the LLM
|
| 42 |
+
messages = [
|
| 43 |
+
{"role": "system", "content": f"{self.system_prompt}\n\nContext from WebAIM resources:\n{context}"}
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
# Add conversation history
|
| 47 |
+
for human, assistant in history:
|
| 48 |
+
messages.append({"role": "user", "content": human})
|
| 49 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 50 |
+
|
| 51 |
+
# Add current message
|
| 52 |
+
messages.append({"role": "user", "content": message})
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
response = self.client.chat_completion(
|
| 56 |
+
messages=messages,
|
| 57 |
+
max_tokens=1500,
|
| 58 |
+
temperature=0.7,
|
| 59 |
+
top_p=0.9
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
assistant_response = response.choices[0].message.content
|
| 63 |
+
|
| 64 |
+
# Add source information
|
| 65 |
+
if relevant_content:
|
| 66 |
+
sources = self.format_sources(relevant_content)
|
| 67 |
+
assistant_response += f"\n\n**Sources:**\n{sources}"
|
| 68 |
+
|
| 69 |
+
return assistant_response
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"I apologize, but I'm experiencing technical difficulties. Please try again. Error: {str(e)}"
|
| 73 |
+
|
| 74 |
+
def format_sources(self, content_list):
|
| 75 |
+
"""Format source citations for display"""
|
| 76 |
+
sources = []
|
| 77 |
+
seen_sources = set()
|
| 78 |
+
|
| 79 |
+
for item in content_list:
|
| 80 |
+
source_key = f"{item['source_file']}_{item['page_number']}"
|
| 81 |
+
if source_key not in seen_sources:
|
| 82 |
+
sources.append(f"โข {item['source_file']} (Page {item['page_number']})")
|
| 83 |
+
seen_sources.add(source_key)
|
| 84 |
+
|
| 85 |
+
return "\n".join(sources)
|
| 86 |
+
|
| 87 |
+
# Initialize chatbot
|
| 88 |
+
chatbot = AccessibilityChatbot()
|
| 89 |
+
|
| 90 |
+
# Create Gradio interface
|
| 91 |
+
def create_interface():
|
| 92 |
+
with gr.Blocks(
|
| 93 |
+
title="Web Accessibility Learning Assistant",
|
| 94 |
+
theme=gr.themes.Soft(),
|
| 95 |
+
css="""
|
| 96 |
+
.gradio-container {
|
| 97 |
+
max-width: 1000px !important;
|
| 98 |
+
}
|
| 99 |
+
"""
|
| 100 |
+
) as demo:
|
| 101 |
+
|
| 102 |
+
gr.Markdown("""
|
| 103 |
+
# ๐ Web Accessibility Learning Assistant
|
| 104 |
+
|
| 105 |
+
Welcome to your personal web accessibility tutor! I'm here to help you learn about web accessibility using **WebAIM resources** - the gold standard for accessibility guidance.
|
| 106 |
+
|
| 107 |
+
## What I can help you with:
|
| 108 |
+
- **WCAG Guidelines**: Understanding success criteria and implementation
|
| 109 |
+
- **Screen Reader Testing**: How to test with assistive technologies
|
| 110 |
+
- **Code Examples**: Accessible HTML, CSS, and JavaScript patterns
|
| 111 |
+
- **Best Practices**: Real-world accessibility solutions
|
| 112 |
+
- **Assignment Help**: Guidance for your accessibility projects
|
| 113 |
+
|
| 114 |
+
*All answers are based on authoritative WebAIM documentation with proper citations.*
|
| 115 |
+
""")
|
| 116 |
+
|
| 117 |
+
# Main chat interface
|
| 118 |
+
chatbot_interface = gr.Chatbot(
|
| 119 |
+
height=500,
|
| 120 |
+
placeholder="๐ Ask me anything about web accessibility!",
|
| 121 |
+
show_label=False,
|
| 122 |
+
container=True,
|
| 123 |
+
bubble_full_width=False
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
msg = gr.Textbox(
|
| 127 |
+
placeholder="Type your question here... (e.g., 'How do I write good alt text?')",
|
| 128 |
+
label="Your Question",
|
| 129 |
+
lines=2,
|
| 130 |
+
max_lines=4
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# Quick start examples
|
| 134 |
+
gr.Markdown("### ๐ Quick Start Examples:")
|
| 135 |
+
gr.Examples(
|
| 136 |
+
examples=[
|
| 137 |
+
"What are the WCAG 2.1 AA requirements for color contrast?",
|
| 138 |
+
"How do I make forms accessible to screen readers?",
|
| 139 |
+
"What's the difference between aria-label and aria-labelledby?",
|
| 140 |
+
"How can I test my website with a screen reader?",
|
| 141 |
+
"What are the most common accessibility mistakes students make?",
|
| 142 |
+
"How do I write effective alt text for complex images?",
|
| 143 |
+
"What ARIA roles should I use for a navigation menu?",
|
| 144 |
+
"How do I make data tables accessible?",
|
| 145 |
+
"What are the keyboard navigation requirements?",
|
| 146 |
+
"How do I ensure my site works without JavaScript?"
|
| 147 |
+
],
|
| 148 |
+
inputs=msg,
|
| 149 |
+
examples_per_page=5
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
# Additional resources
|
| 153 |
+
with gr.Accordion("๐ Additional Learning Resources", open=False):
|
| 154 |
+
gr.Markdown("""
|
| 155 |
+
### Recommended Tools:
|
| 156 |
+
- **WAVE**: Web accessibility evaluation tool
|
| 157 |
+
- **axe DevTools**: Browser extension for accessibility testing
|
| 158 |
+
- **Lighthouse**: Built-in accessibility audit in Chrome
|
| 159 |
+
- **NVDA/JAWS**: Screen readers for testing
|
| 160 |
+
|
| 161 |
+
### Key Standards:
|
| 162 |
+
- **WCAG 2.1**: Current web accessibility guidelines
|
| 163 |
+
- **Section 508**: US federal accessibility requirements
|
| 164 |
+
- **ADA**: Americans with Disabilities Act considerations
|
| 165 |
+
""")
|
| 166 |
+
|
| 167 |
+
# Handle message submission
|
| 168 |
+
def respond(message, history):
|
| 169 |
+
if not message.strip():
|
| 170 |
+
return history, ""
|
| 171 |
+
|
| 172 |
+
response = chatbot.generate_response(message, history)
|
| 173 |
+
history.append((message, response))
|
| 174 |
+
return history, ""
|
| 175 |
+
|
| 176 |
+
msg.submit(respond, [msg, chatbot_interface], [chatbot_interface, msg])
|
| 177 |
+
|
| 178 |
+
# Footer
|
| 179 |
+
gr.Markdown("""
|
| 180 |
+
---
|
| 181 |
+
*This chatbot uses WebAIM resources and is powered by DeepSeek-R1.
|
| 182 |
+
For the most up-to-date information, always refer to the original WebAIM documentation.*
|
| 183 |
+
""")
|
| 184 |
+
|
| 185 |
+
return demo
|
| 186 |
+
|
| 187 |
+
# Launch the app
|
| 188 |
+
if __name__ == "__main__":
|
| 189 |
+
demo = create_interface()
|
| 190 |
+
demo.launch()
|