datascientist22 commited on
Commit
eaf4e19
1 Parent(s): 253e08c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -36
app.py CHANGED
@@ -1,13 +1,19 @@
1
  import streamlit as st
2
- import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
4
 
5
- # Load the tokenizer and model using PyTorch
6
  tokenizer = AutoTokenizer.from_pretrained("MohamedMotaz/Examination-llama-8b-4bit")
7
- model = AutoModelForCausalLM.from_pretrained("MohamedMotaz/Examination-llama-8b-4bit", torch_dtype=torch.float16).to("cuda" if torch.cuda.is_available() else "cpu")
 
 
 
 
 
 
8
 
9
  # App Title
10
- st.title("Exam Corrector: Automated Grading with LLama 8b Model (PyTorch)")
11
 
12
  # Instructions
13
  st.markdown("""
@@ -20,46 +26,18 @@ st.markdown("""
20
  model_answer = st.text_area("Model Answer", "The process of photosynthesis involves converting light energy into chemical energy.")
21
  student_answer = st.text_area("Student Answer", "Photosynthesis is when plants turn light into energy.")
22
 
23
- # Display documentation in the app
24
- with st.expander("Click to View Documentation"):
25
- st.markdown("""
26
- ## Exam-Corrector: A Fine-tuned LLama 8b Model
27
-
28
- Exam-corrector is a fine-tuned version of the LLama 8b model, specifically adapted to function as a written question corrector. This model grades student answers by comparing them against model answers using predefined instructions.
29
-
30
- ### Model Description:
31
- The model ensures consistent and fair grading for written answers. Full marks are given to student answers that convey the complete meaning of the model answer, even with different wording.
32
-
33
- ### Grading Instructions:
34
- - Model Answer is only used as a reference and does not receive marks.
35
- - Full marks are awarded when student answers convey the full meaning of the model answer.
36
- - Partial marks are deducted for incomplete or irrelevant information.
37
-
38
- ### Input Format:
39
- - **Model Answer**: {model_answer}
40
- - **Student Answer**: {student_answer}
41
-
42
- ### Output Format:
43
- - **Grade**: {grade}
44
- - **Explanation**: {explanation}
45
-
46
- ### Training Details:
47
- - Fine-tuned with LoRA (Low-Rank Adaptation).
48
- - Percentage of trainable model parameters: 3.56%.
49
- """)
50
-
51
  # Button to trigger grading
52
  if st.button("Grade Answer"):
53
  # Combine inputs into the required prompt format
54
  inputs = f"Model Answer: {model_answer}\n\nStudent Answer: {student_answer}\n\nResponse:"
55
-
56
  # Tokenize the inputs using PyTorch tensors
57
- input_ids = tokenizer(inputs, return_tensors="pt").input_ids.to(model.device)
58
 
59
- # Generate the response using the model (PyTorch)
60
  with torch.no_grad():
61
  outputs = model.generate(input_ids, max_length=200)
62
-
63
  # Decode the output
64
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
65
 
 
1
  import streamlit as st
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
 
5
+ # Load the tokenizer and model for CPU (avoid bitsandbytes quantization)
6
  tokenizer = AutoTokenizer.from_pretrained("MohamedMotaz/Examination-llama-8b-4bit")
7
+ model = AutoModelForCausalLM.from_pretrained(
8
+ "MohamedMotaz/Examination-llama-8b-4bit",
9
+ torch_dtype=torch.float32 # Use float32 to avoid 8-bit quantization
10
+ )
11
+
12
+ # Ensure the model runs on CPU
13
+ model = model.to("cpu")
14
 
15
  # App Title
16
+ st.title("Exam Corrector: Automated Grading with LLama 8b Model (CPU)")
17
 
18
  # Instructions
19
  st.markdown("""
 
26
  model_answer = st.text_area("Model Answer", "The process of photosynthesis involves converting light energy into chemical energy.")
27
  student_answer = st.text_area("Student Answer", "Photosynthesis is when plants turn light into energy.")
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # Button to trigger grading
30
  if st.button("Grade Answer"):
31
  # Combine inputs into the required prompt format
32
  inputs = f"Model Answer: {model_answer}\n\nStudent Answer: {student_answer}\n\nResponse:"
33
+
34
  # Tokenize the inputs using PyTorch tensors
35
+ input_ids = tokenizer(inputs, return_tensors="pt").input_ids.to("cpu")
36
 
37
+ # Generate the response using the model (PyTorch, CPU-based)
38
  with torch.no_grad():
39
  outputs = model.generate(input_ids, max_length=200)
40
+
41
  # Decode the output
42
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
43