Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,94 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
-
import cv2
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import pytesseract
|
| 8 |
-
from sentence_transformers import SentenceTransformer, util
|
| 9 |
-
import io
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
device
|
| 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 |
-
return
|
| 46 |
-
elif similarity_score >= 0.
|
| 47 |
-
return
|
| 48 |
-
|
| 49 |
-
return
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
response
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
response
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import pytesseract
|
| 8 |
+
from sentence_transformers import SentenceTransformer, util
|
| 9 |
+
import io
|
| 10 |
+
|
| 11 |
+
model_name = "eachadea/vicuna-7b-1.1"
|
| 12 |
+
|
| 13 |
+
# Check if CUDA is available, otherwise, fall back to CPU
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
print(f"Using device: {device}")
|
| 16 |
+
|
| 17 |
+
# Load the tokenizer
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 19 |
+
|
| 20 |
+
# Load the model
|
| 21 |
+
# If CUDA is available, use float16, otherwise, use float32
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_name,
|
| 24 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 25 |
+
device_map="auto" if device == "cuda" else None
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Move model to the appropriate device (CPU or CUDA)
|
| 29 |
+
model.to(device)
|
| 30 |
+
|
| 31 |
+
# Load a smaller version of Sentence-BERT model
|
| 32 |
+
model1 = SentenceTransformer('all-MiniLM-L6-v2')
|
| 33 |
+
|
| 34 |
+
def get_embedding(text):
|
| 35 |
+
return model1.encode(text, convert_to_tensor=True)
|
| 36 |
+
|
| 37 |
+
def calculate_similarity(text1, text2):
|
| 38 |
+
embedding1 = get_embedding(text1)
|
| 39 |
+
embedding2 = get_embedding(text2)
|
| 40 |
+
similarity = util.pytorch_cos_sim(embedding1, embedding2)
|
| 41 |
+
return similarity.item()
|
| 42 |
+
|
| 43 |
+
def get_grade(similarity_score):
|
| 44 |
+
if similarity_score >= 0.9:
|
| 45 |
+
return 5
|
| 46 |
+
elif similarity_score >= 0.8:
|
| 47 |
+
return 4
|
| 48 |
+
elif similarity_score >= 0.7:
|
| 49 |
+
return 3
|
| 50 |
+
elif similarity_score >= 0.6:
|
| 51 |
+
return 2
|
| 52 |
+
else:
|
| 53 |
+
return 1
|
| 54 |
+
|
| 55 |
+
def extract_text_from_image(image):
|
| 56 |
+
# Convert PIL image to RGB format
|
| 57 |
+
image = image.convert('RGB')
|
| 58 |
+
# Use pytesseract to extract text from the image
|
| 59 |
+
text = pytesseract.image_to_string(image)
|
| 60 |
+
return text.strip()
|
| 61 |
+
|
| 62 |
+
def evaluate_answer(image):
|
| 63 |
+
student_answer = extract_text_from_image(image)
|
| 64 |
+
model_answer = "The process of photosynthesis helps plants produce glucose using sunlight."
|
| 65 |
+
similarity_score = calculate_similarity(student_answer, model_answer)
|
| 66 |
+
grade = get_grade(similarity_score)
|
| 67 |
+
feedback = f"Student's answer: {student_answer}\nTeacher's answer: {model_answer}"
|
| 68 |
+
return grade, similarity_score * 100, feedback
|
| 69 |
+
|
| 70 |
+
def generate_response(prompt):
|
| 71 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 72 |
+
|
| 73 |
+
# Generate response from the model
|
| 74 |
+
with torch.no_grad():
|
| 75 |
+
outputs = model.generate(inputs.input_ids, max_length=150, temperature=0.7)
|
| 76 |
+
|
| 77 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 78 |
+
return response
|
| 79 |
+
|
| 80 |
+
def gradio_interface(image, prompt):
|
| 81 |
+
grade, similarity_score, feedback = evaluate_answer(image)
|
| 82 |
+
response = generate_response(prompt)
|
| 83 |
+
return grade, similarity_score, feedback, response
|
| 84 |
+
|
| 85 |
+
# Define Gradio interface
|
| 86 |
+
interface = gr.Interface(
|
| 87 |
+
fn=gradio_interface,
|
| 88 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(lines=2, placeholder="Enter your prompt here")],
|
| 89 |
+
outputs=[gr.Label(), gr.Label(), gr.Textbox(), gr.Textbox()],
|
| 90 |
+
live=True
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
interface.launch()
|