Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,80 +2,80 @@
|
|
| 2 |
# UNEB Primary 7/6 Exam Preparation Platform
|
| 3 |
# Multi-AI System: Gemini (Primary) β Cohere β HF (Fallback)
|
| 4 |
# ========================================================
|
| 5 |
-
|
| 6 |
import os
|
| 7 |
import json
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
submit_btn = gr.Button(" Submit for Correction", variant="primary", size="lg")
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
model="command-r-plus-08-2024",
|
| 80 |
message=prompt,
|
| 81 |
temperature=temperature
|
|
@@ -85,7 +85,7 @@ import json
|
|
| 85 |
print(f"β Cohere attempt {attempt+1} failed: {e}")
|
| 86 |
if attempt < max_retries - 1:
|
| 87 |
time.sleep(1)
|
| 88 |
-
|
| 89 |
# Try Hugging Face (Fallback) - text only
|
| 90 |
if hf_client:
|
| 91 |
try:
|
|
@@ -98,7 +98,7 @@ import json
|
|
| 98 |
return completion.choices[0].message.content, "hf"
|
| 99 |
except Exception as e:
|
| 100 |
print(f"β HF failed: {e}")
|
| 101 |
-
|
| 102 |
return " All AI services failed. Please try again later.", "error"
|
| 103 |
|
| 104 |
# ---------- 3. Uganda Primary Curriculum Syllabus (P6 & P7 Only, by Subject) ----------
|
|
|
|
| 2 |
# UNEB Primary 7/6 Exam Preparation Platform
|
| 3 |
# Multi-AI System: Gemini (Primary) β Cohere β HF (Fallback)
|
| 4 |
# ========================================================
|
|
|
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import time
|
| 10 |
+
from io import BytesIO
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import csv
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
import re
|
| 15 |
+
|
| 16 |
+
# ---------- 1. Configure AI Systems ----------
|
| 17 |
+
try:
|
| 18 |
+
import google.generativeai as genai
|
| 19 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 20 |
+
gemini_model = genai.GenerativeModel('gemini-2.0-flash-exp')
|
| 21 |
+
print("β Gemini AI initialized (PRIMARY with Vision)")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"β Gemini Error: {e}")
|
| 24 |
+
gemini_model = None
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
import cohere
|
| 28 |
+
cohere_client = cohere.Client(os.getenv("COHERE_API_KEY"))
|
| 29 |
+
print("β Cohere initialized (SECONDARY)")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"β Cohere Error: {e}")
|
| 32 |
+
cohere_client = None
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
from huggingface_hub import InferenceClient
|
| 36 |
+
hf_client = InferenceClient(api_key=os.environ.get("HF_TOKEN"))
|
| 37 |
+
print("β Hugging Face initialized (FALLBACK)")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"β HF Error: {e}")
|
| 40 |
+
hf_client = None
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def ask_ai(prompt, temperature=0.7, max_retries=2, image=None):
|
| 44 |
+
"""Try models: Gemini β Cohere β HF
|
| 45 |
+
If image provided, only Gemini can process it"""
|
| 46 |
+
|
| 47 |
+
# Try Gemini first (Primary) - handles both text and images
|
| 48 |
+
if gemini_model:
|
| 49 |
+
for attempt in range(max_retries):
|
| 50 |
+
try:
|
| 51 |
+
if image:
|
| 52 |
+
response = gemini_model.generate_content(
|
| 53 |
+
[prompt, image],
|
| 54 |
+
generation_config=genai.types.GenerationConfig(
|
| 55 |
+
temperature=temperature,
|
| 56 |
+
)
|
| 57 |
+
)
|
| 58 |
+
else:
|
| 59 |
+
response = gemini_model.generate_content(
|
| 60 |
+
prompt,
|
| 61 |
+
generation_config=genai.types.GenerationConfig(
|
| 62 |
+
temperature=temperature,
|
| 63 |
+
)
|
| 64 |
+
)
|
| 65 |
+
return response.text, "gemini"
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"β Gemini attempt {attempt+1} failed: {e}")
|
| 68 |
+
if attempt < max_retries - 1:
|
| 69 |
+
time.sleep(1)
|
| 70 |
|
| 71 |
+
if image:
|
| 72 |
+
return " Image analysis requires Gemini AI. Please check API configuration.", "error"
|
|
|
|
| 73 |
|
| 74 |
+
# Try Cohere (Secondary) - text only
|
| 75 |
+
if cohere_client:
|
| 76 |
+
for attempt in range(max_retries):
|
| 77 |
+
try:
|
| 78 |
+
response = cohere_client.chat(
|
| 79 |
model="command-r-plus-08-2024",
|
| 80 |
message=prompt,
|
| 81 |
temperature=temperature
|
|
|
|
| 85 |
print(f"β Cohere attempt {attempt+1} failed: {e}")
|
| 86 |
if attempt < max_retries - 1:
|
| 87 |
time.sleep(1)
|
| 88 |
+
|
| 89 |
# Try Hugging Face (Fallback) - text only
|
| 90 |
if hf_client:
|
| 91 |
try:
|
|
|
|
| 98 |
return completion.choices[0].message.content, "hf"
|
| 99 |
except Exception as e:
|
| 100 |
print(f"β HF failed: {e}")
|
| 101 |
+
|
| 102 |
return " All AI services failed. Please try again later.", "error"
|
| 103 |
|
| 104 |
# ---------- 3. Uganda Primary Curriculum Syllabus (P6 & P7 Only, by Subject) ----------
|