Garvitj commited on
Commit
f7dfa37
·
verified ·
1 Parent(s): f73e510

Update app.py

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