Spaces:
Running
Running
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) |