RISE / rise_colab_api.py
bChaukar's picture
Update rise_colab_api.py
9a71be7 verified
Raw History Blame Contribute Delete
9.25 kB
"""RISE reading-assistant API for a Google Colab backend.
Uses RISE's own trained encoder-decoder Transformer, Whisper Small,
gTTS pronunciation audio, and MediaPipe Hand Landmarker.
No notebook-only output.eval_js or IPython.display calls are used in API routes.
"""
from __future__ import annotations
import base64
import io
import os
import re
import random
import tempfile
import subprocess
import time
import uuid
import urllib.request
from pathlib import Path
from difflib import SequenceMatcher
from typing import Any
import numpy as np
import soundfile as sf
import torch
import cv2
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from gtts import gTTS
from transformers import pipeline as hf_pipeline
from rise_custom_transformer import load_custom_text_generator
# --------------------------- Curriculum/configuration -----------------------
ADVANCE_THRESHOLD = 80.0
LEVELS = ["beginner", "elementary", "intermediate", "upper_intermediate", "advanced"]
PARTS = ["sentence", "paragraph", "story"]
NUM_SENTENCES = 3
SAMPLE_RATE = 16000
AUDIO_DIR = Path(os.environ.get("RISE_AUDIO_DIR", "/content/reading_assistant_audio"))
AUDIO_DIR.mkdir(parents=True, exist_ok=True)
LEVEL_SPECS = {
"beginner": {"sentence_words": (4, 7), "paragraph_words": 40, "story_paragraphs": 3, "story_words_per_paragraph": 38},
"elementary": {"sentence_words": (7, 10), "paragraph_words": 60, "story_paragraphs": 3, "story_words_per_paragraph": 35},
"intermediate": {"sentence_words": (10, 14), "paragraph_words": 80, "story_paragraphs": 4, "story_words_per_paragraph": 34},
"upper_intermediate": {"sentence_words": (14, 18), "paragraph_words": 90, "story_paragraphs": 4, "story_words_per_paragraph": 38},
"advanced": {"sentence_words": (18, 24), "paragraph_words": 110, "story_paragraphs": 5, "story_words_per_paragraph": 34},
}
LEVEL_GUIDANCE = {
"beginner": "Use very common everyday words, concrete topics, and short, simple sentences.",
"elementary": "Use familiar vocabulary, clear sentences, and a little descriptive detail.",
"intermediate": "Use moderately varied vocabulary, connected ideas, and some compound sentences.",
"upper_intermediate": "Use varied vocabulary, descriptive details, and more complex sentence structures.",
"advanced": "Use sophisticated vocabulary, nuanced ideas, varied syntax, and complex but readable sentences.",
}
FALLBACK = {
"beginner": {
"sentence": ["The cat sat on the mat. The dog ran to the door. The sun is very bright today."],
"paragraph": ["Mia went to the park with her brother. They played with a red ball near a large tree. After a while, they sat on a bench and drank some water. They walked home happily before dinner."],
"story": ["One sunny morning, Leo found a small turtle near a pond. He carried it carefully to his garden. His mother helped him make a safe place for it. They gave the turtle water and fresh leaves. After a few days, it was strong and active. Leo took it back to the pond and watched it swim away."]},
"elementary": {
"sentence": ["The children built a sandcastle near the edge of the water. She forgot her umbrella and got soaked in the rain. He carefully wrapped the gift before placing it under the tree."],
"paragraph": ["On Sunday, Ana and her father visited the library. They each chose three books and sat near a sunny window. Ana read a story about animals while her father read about history. On the way home, they talked about their favourite parts. The visit became a happy weekly tradition."],
"story": ["Nina had never travelled by train before. Her aunt invited her to visit the coast for the day. Nina watched fields and small towns pass the window. When the sea appeared, she smiled with excitement. They built a sandcastle and ate lunch on the beach. Nina fell asleep on the journey home, remembering the wonderful day."]},
"intermediate": {
"sentence": ["Although the weather looked threatening, they decided to continue with the picnic. She had practised the violin for years before performing on stage. The experiment produced unexpected results that challenged several established theories."],
"paragraph": ["The restoration of the old theatre took nearly four years to complete. Workers uncovered layers of paint that revealed how the building had changed over time. Beneath the stage, they found programmes, handwritten notes, and an old leather glove. The architect said the project had changed her understanding of history. When the theatre reopened, every performance sold out."],
"story": ["For many years, the lighthouse at the edge of the peninsula had guided ships safely home. A young historian named Clara was asked to research its past. She discovered old logbooks and letters hidden behind a loose stone. The letters belonged to the former keeper's daughter, who had moved away decades earlier. Clara found the woman and returned the letters to her. Together, they read about a life shaped by storms, patience, and the sea."]},
"upper_intermediate": {
"sentence": ["The proposal, while ambitious in scope, failed to account for the considerable logistical challenges involved. The relationship between language and identity is more complex than introductory accounts suggest. Her reluctance to commit to a definitive position frustrated colleagues who valued clarity above nuance."],
"paragraph": ["The concept of deep time can be profoundly disorienting when considered carefully. Human civilisation occupies only a tiny fraction of Earth's long history. Mountains that appear permanent have risen and eroded repeatedly, while oceans have advanced across continents. Understanding these changes requires intellectual humility. It reminds us that the scale of human experience is small compared with the age of the world we inhabit."],
"story": ["Two neighbours had not spoken for eleven years after a disagreement about a boundary wall. Neither could remember exactly how the argument had begun, and the wall had since been rebuilt. During a winter storm, a tree fell across their shared fence. Removing it required them to coordinate their efforts. They met with tools and began clearing the branches together. At first, their conversation was awkward, but practical questions soon became ordinary conversation. The following Sunday, they shared coffee beside the repaired fence."]},
"advanced": {
"sentence": ["The epistemological assumptions underlying the study were never made explicit, undermining the credibility of its conclusions. Language does not merely describe experience; it shapes the categories through which experience becomes available to consciousness. What appears from a distance as consensus often dissolves into a negotiation between competing interests."],
"paragraph": ["The question of what we owe to future generations is philosophically formidable because those people do not yet exist to articulate their interests. We cannot consult them or know with precision what they will value, and the world they inherit may differ substantially from our own. Yet uncertainty does not automatically dissolve our obligations. It makes fulfilling them require moral imagination, careful judgement, and a willingness to consider consequences beyond the limits of immediate experience."],
"story": ["The translator spent twelve years working on a medieval chronicle written in a dialect for which no modern dictionary existed. She reconstructed its grammar from patterns within the text, comparing repeated phrases and unusual word endings. When the translation appeared, specialists identified several errors. She acknowledged each one in later editions and explained why her original decisions had seemed reasonable. The chronicle itself attracted only a small academic audience. However, her preface became widely read because it described uncertainty without pretending that every question had a simple answer. Students continued to study it as an example of intellectual honesty."]},
}
# --------------------------- Model globals ----------------------------------
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
GENAI_MODEL_NAME = "RISE Custom Encoder-Decoder Transformer"
custom_text_generator = None
speech_to_text = None
_hand_landmarker = None
model_status: dict[str, Any] = {"whisper": False, "text_generation": False, "gesture": False, "text_model": None, "errors": []}
generation_records: list[dict[str, Any]] = []
sessions: dict[str, dict[str, Any]] = {}
_CONTRACTIONS = {
"i'm": "i am", "i've": "i have", "i'll": "i will", "i'd": "i would",
"it's": "it is", "that's": "that is", "he's": "he is", "she's": "she is",
"we're": "we are", "they're": "they are", "you're": "you are",
"isn't": "is not", "aren't": "are not", "wasn't": "was not", "weren't": "were not",
"don't": "do not", "doesn't": "does not", "didn't": "did not", "can't": "cannot",
"couldn't": "could not", "won't": "will not", "wouldn't": "would not", "there's": "there is",