mikaelJ46 commited on
Commit
e40a5cc
Β·
verified Β·
1 Parent(s): ea28ecd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -74
app.py CHANGED
@@ -5,81 +5,77 @@
5
 
6
  import os
7
  import json
8
- from datetime import datetime
9
- import gradio as gr
10
- import time
11
- from io import BytesIO
12
- from PIL import Image
13
- import csv
14
- from pathlib import Path
15
- import re
16
-
17
- # ---------- 1. Configure AI Systems ----------
18
- # Primary: Gemini (with vision support)
19
- try:
20
- import google.generativeai as genai
21
- genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
22
- gemini_model = genai.GenerativeModel('gemini-2.0-flash-exp')
23
- print("βœ“ Gemini AI initialized (PRIMARY with Vision)")
24
- except Exception as e:
25
- print(f"βœ— Gemini Error: {e}")
26
- gemini_model = None
27
-
28
- # Secondary: Cohere
29
- try:
30
- import cohere
31
- cohere_client = cohere.Client(os.getenv("COHERE_API_KEY"))
32
- print("βœ“ Cohere initialized (SECONDARY)")
33
- except Exception as e:
34
- print(f"βœ— Cohere Error: {e}")
35
- cohere_client = None
36
-
37
- # Tertiary: Hugging Face Inference
38
- try:
39
- from huggingface_hub import InferenceClient
40
- hf_client = InferenceClient(api_key=os.environ.get("HF_TOKEN"))
41
- print("βœ“ Hugging Face initialized (FALLBACK)")
42
- except Exception as e:
43
- print(f"βœ— HF Error: {e}")
44
- hf_client = None
45
-
46
- # ---------- 2. Unified AI Function ----------
47
- def ask_ai(prompt, temperature=0.7, max_retries=2, image=None):
48
- """Try models: Gemini β†’ Cohere β†’ HF
49
- If image provided, only Gemini can process it"""
50
-
51
- # Try Gemini first (Primary) - handles both text and images
52
- if gemini_model:
53
- for attempt in range(max_retries):
54
- try:
55
- if image:
56
- response = gemini_model.generate_content(
57
- [prompt, image],
58
- generation_config=genai.types.GenerationConfig(
59
- temperature=temperature,
60
- )
61
- )
62
- else:
63
- response = gemini_model.generate_content(
64
- prompt,
65
- generation_config=genai.types.GenerationConfig(
66
- temperature=temperature,
67
- )
 
 
 
 
 
 
 
 
 
 
68
  )
69
- return response.text, "gemini"
70
- except Exception as e:
71
- print(f"βœ— Gemini attempt {attempt+1} failed: {e}")
72
- if attempt < max_retries - 1:
73
- time.sleep(1)
74
-
75
- if image:
76
- return " Image analysis requires Gemini AI. Please check API configuration.", "error"
77
-
78
- # Try Cohere (Secondary) - text only
79
- if cohere_client:
80
- for attempt in range(max_retries):
81
- try:
82
- response = cohere_client.chat(
83
  model="command-r-plus-08-2024",
84
  message=prompt,
85
  temperature=temperature
 
5
 
6
  import os
7
  import json
8
+ with gr.Tabs():
9
+ with gr.Tab("✏️ Draw Answers"):
10
+ canvas = gr.Sketchpad(
11
+ label="Draw or write your answers here",
12
+ type="pil",
13
+ height=500,
14
+ brush=gr.Brush(
15
+ colors=["#000000", "#0000FF", "#FF0000"],
16
+ default_size=4
17
+ )
18
+ )
19
+ canvas_status = gr.Textbox(
20
+ label="Canvas Status",
21
+ interactive=False,
22
+ value="Ready to draw"
23
+ )
24
+
25
+ with gr.Tab("πŸ“Έ Upload Photo"):
26
+ upload_image = gr.Image(
27
+ label="Upload photo of your written work",
28
+ type="pil",
29
+ height=500
30
+ )
31
+ upload_status = gr.Textbox(
32
+ label="Upload Status",
33
+ interactive=False,
34
+ value="Ready to upload"
35
+ )
36
+
37
+ with gr.Tab(" Type Answers"):
38
+ gr.Markdown("""
39
+ ### πŸ“ Type Your Answers
40
+ **Instructions:** Type each answer in the box below. Use one of these formats:
41
+ - **Option A (Recommended):** Type answers separated by blank lines
42
+ ```
43
+ Answer to question 1 goes here
44
+
45
+ Answer to question 2 goes here
46
+
47
+ Answer to question 3 goes here
48
+ ```
49
+ - **Option B:** Type with labels
50
+ ```
51
+ Answer 1: Your answer here
52
+ Answer 2: Your answer here
53
+ Answer 3: Your answer here
54
+ ```
55
+ - **Option C:** Type with Q labels
56
+ ```
57
+ Q1: Your answer here
58
+ Q2: Your answer here
59
+ Q3: Your answer here
60
+ ```
61
+ """)
62
+
63
+ typed_answers = gr.Textbox(
64
+ label="Type Your Answers Here",
65
+ lines=20,
66
+ placeholder="Type your answers using any of the formats shown above...",
67
+ elem_classes="answer-input"
68
+ )
69
+
70
+ # Combined submit button (outside tabs, still inside right column)
71
+ with gr.Row():
72
+ submit_btn = gr.Button(" Submit for Correction", variant="primary", size="lg")
73
+
74
+ submit_status = gr.Textbox(
75
+ label="Submission Status",
76
+ interactive=False,
77
+ lines=2
78
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  model="command-r-plus-08-2024",
80
  message=prompt,
81
  temperature=temperature