Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Demo script for the Handwriting Assessment App | |
| Creates actual images and processes them through the Gemini API to demonstrate real OCR functionality. | |
| """ | |
| import tempfile | |
| import os | |
| from app import HandwritingRecognizer, TextScorer | |
| from config import GEMINI_API_KEY, GEMINI_MODEL | |
| from sample_images import create_sample_answer_key_image, create_sample_student_response_image | |
| def run_demo(): | |
| """Run a demo that actually calls the Gemini API to process images.""" | |
| print("🎯 Handwriting Assessment App - Real API Demo") | |
| print("=" * 60) | |
| # Check if API key is available | |
| if not GEMINI_API_KEY: | |
| print("❌ Error: No Gemini API key found!") | |
| print(" Please set GEMINI_API_KEY in your .env file") | |
| print(" Get your API key from: https://makersuite.google.com/app/apikey") | |
| return | |
| print(f"✅ API Key loaded: {GEMINI_API_KEY[:20]}...") | |
| print(f"🤖 Using model: {GEMINI_MODEL} (supports 768x768 image processing)") | |
| try: | |
| # Initialize the recognizer | |
| print("\n1. Initializing Gemini API...") | |
| recognizer = HandwritingRecognizer(GEMINI_API_KEY) | |
| print(" ✅ API initialized successfully") | |
| # Generate sample images | |
| print("\n2. Generating sample handwritten images...") | |
| answer_key_img = create_sample_answer_key_image() | |
| student_response_img = create_sample_student_response_image() | |
| print(" ✅ Sample images created") | |
| # Save images temporarily for display purposes | |
| with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp1: | |
| answer_key_img.save(tmp1.name) | |
| answer_key_path = tmp1.name | |
| with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp2: | |
| student_response_img.save(tmp2.name) | |
| student_response_path = tmp2.name | |
| print(f" 📁 Answer key image saved: {answer_key_path}") | |
| print(f" 📁 Student response image saved: {student_response_path}") | |
| # Process answer key through OCR | |
| print("\n3. Processing answer key through Gemini OCR...") | |
| answer_key_text = recognizer.extract_text_from_image(answer_key_img) | |
| print(" ✅ Answer key processed") | |
| print(" 📄 Extracted text:") | |
| print(" " + "-" * 40) | |
| for line in answer_key_text.split('\n'): | |
| print(f" {line}") | |
| # Process student response through OCR | |
| print("\n4. Processing student response through Gemini OCR...") | |
| student_response_text = recognizer.extract_text_from_image(student_response_img) | |
| print(" ✅ Student response processed") | |
| print(" 📄 Extracted text:") | |
| print(" " + "-" * 40) | |
| for line in student_response_text.split('\n'): | |
| print(f" {line}") | |
| # Calculate similarity score | |
| print("\n5. Calculating similarity score...") | |
| score, metrics = TextScorer.calculate_similarity(answer_key_text, student_response_text) | |
| interpretation, color = TextScorer.get_score_interpretation(score) | |
| print(f"\n📊 RESULTS:") | |
| print(f" Overall Score: {score:.1%}") | |
| print(f" Interpretation: {interpretation}") | |
| print(f" Sequence Similarity: {metrics['sequence_similarity']:.1%}") | |
| print(f" Word Similarity: {metrics['word_similarity']:.1%}") | |
| print(f" Character Similarity: {metrics['char_similarity']:.1%}") | |
| print(f"\n✅ Real API demo completed successfully!") | |
| print(" 🖼️ Sample images were processed through actual Gemini OCR") | |
| print(f" 🤖 Text extraction used {GEMINI_MODEL} with 768x768 image processing") | |
| print(" 📊 Scoring was calculated from real OCR results") | |
| print("\n To use the full Gradio interface, run: python app.py") | |
| # Clean up temporary files | |
| try: | |
| os.unlink(answer_key_path) | |
| os.unlink(student_response_path) | |
| except: | |
| pass | |
| except Exception as e: | |
| print(f"\n❌ Error during demo: {str(e)}") | |
| print(" This might be due to:") | |
| print(" - Invalid API key") | |
| print(" - Network connectivity issues") | |
| print(" - API quota exceeded") | |
| print(" - Model availability issues") | |
| if __name__ == "__main__": | |
| run_demo() |