File size: 1,912 Bytes
4b00165
253e08c
eaf4e19
4b00165
4ee92ad
4b00165
4ee92ad
 
eaf4e19
4ee92ad
 
 
eaf4e19
 
4b00165
eaf4e19
4b00165
 
 
 
253e08c
4b00165
 
 
 
 
 
 
 
 
 
 
eaf4e19
253e08c
4ee92ad
 
eaf4e19
253e08c
 
eaf4e19
253e08c
4b00165
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# Load the tokenizer and model for CPU without bitsandbytes
tokenizer = AutoTokenizer.from_pretrained("MohamedMotaz/Examination-llama-8b-4bit")

# Load the model in full precision, explicitly avoiding 8-bit quantization
model = AutoModelForCausalLM.from_pretrained(
    "MohamedMotaz/Examination-llama-8b-4bit",
    torch_dtype=torch.float32,  # Ensure it uses full precision (float32)
    device_map="cpu",  # Force the model to run on the CPU
)

# App Title
st.title("Exam Corrector: Automated Grading with LLama 8b Model (CPU)")

# Instructions
st.markdown("""
### Instructions:
- Enter both the **Model Answer** and the **Student Answer**.
- Click on the **Grade Answer** button to get the grade and explanation.
""")

# Input fields for Model Answer and Student Answer
model_answer = st.text_area("Model Answer", "The process of photosynthesis involves converting light energy into chemical energy.")
student_answer = st.text_area("Student Answer", "Photosynthesis is when plants turn light into energy.")

# Button to trigger grading
if st.button("Grade Answer"):
    # Combine inputs into the required prompt format
    inputs = f"Model Answer: {model_answer}\n\nStudent Answer: {student_answer}\n\nResponse:"

    # Tokenize the inputs using PyTorch tensors
    input_ids = tokenizer(inputs, return_tensors="pt").input_ids

    # Generate the response using the model (PyTorch, CPU-based)
    with torch.no_grad():
        outputs = model.generate(input_ids, max_length=200)

    # Decode the output
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    # Display the grade and explanation
    st.subheader("Grading Results")
    st.write(response)

# Footer and app creator details
st.markdown("""
---
**App created by [Engr. Hamesh Raj](https://www.linkedin.com/in/hamesh-raj)**
""")