Spaces:
Running
Running
File size: 3,305 Bytes
ebd324a e4c5b39 ebd324a 7d06f13 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
import gradio as gr
from transformers import pipeline
from langchain import PromptTemplate
from langchain.document_loaders import WebBaseLoader
from langchain.schema import StrOutputParser
from langchain.schema.prompt_template import format_document
from langchain.chains import LLMChain
from langchain_google_genai import ChatGoogleGenerativeAI
import ast
import os
import getpass
import matplotlib.pyplot as plt
api_key = os.environ.get('GOOGLE_API_KEY')
if api_key is None:
raise ValueError("No API key found. Please set the 'GOOGLE_API_KEY' environment variable.")
os.environ['GOOGLE_API_KEY'] = api_key
# Initialize the OCR pipeline
ocr_pipe = pipeline("image-to-text", model="jinhybr/OCR-Donut-CORD")
# Define the initial prompt for the LLM
initial_prompt = '''
You are an IELTS writing examiner. Below, you will receive a series of details pertaining to a writing task. Please evaluate the writing based on the information provided:
1. The type of test (General or Academic)
2. The task number (Task 1 or Task 2)
3. The specific question for this task
4. The author's written response
Please follow these steps for your evaluation:
1. Provide a score on the IELTS scale (0-9 points).
2. Leave a space.
3. Detail the weaknesses and mistakes in the writing.
Evaluate the writing based on the following four criteria:
- Task Response
- Coherence and Cohesion
- Lexical Resource
- Grammatical Range and Accuracy
Respond in this exact format:
Task type: [task type]
Task number: [task number]
score: [score]
Weaknesses and Mistakes:
[Weaknesses and Mistakes]
Notes:
- The text may have been obtained via OCR, which could result in some errors.
- Disregard any text enclosed in <>. They separate different parts of the text.
- If the Task type is empty, try to identify the task type from the question. If you cannot determine the task type, mention that the task type is unclear.
- If the specific question is not present in the task content, mention that the question does not exist in the task response.
Task type: {task_type}
Task number: {task_number}
Question: {question}
Task content: {content}
'''
# Initialize the LLM
llm_model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.7, top_p=0.85)
# Define the prompt template
prompt = PromptTemplate(input_variables=['task_type', 'task_number', 'question', 'content'], template=initial_prompt)
# Define the LLM chain
chain = LLMChain(
llm=llm_model,
prompt=prompt,
)
def evaluate(task_type, task_number, question, image):
# Process the image to extract text
text_content = ocr_pipe(image)
content = text_content[0]['generated_text']
# Run the chain
result = chain.run({
'task_type': task_type,
'task_number': task_number,
'question': question,
'content': content
})
return result
# Create the Gradio interface
inputs = [
gr.Dropdown(choices=["Academic", "General"], label="Test Type", value="Academic"),
gr.Dropdown(choices=["Task 1", "Task 2"], label="Task Number", value="Task 1"),
gr.Textbox(label="Question", value=""),
gr.Image(type="pil", label="Upload Image")
]
outputs = gr.Markdown(label="Result")
gr.Interface(fn=evaluate, inputs=inputs, outputs=outputs, title="IELTS Writing Evaluation").launch(share=True) |