Spaces:
Sleeping
Sleeping
hosseinhimself
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Untitled13.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1916fw1h61Jt2FokODxZbZXbIRnBrlwM5
|
8 |
+
"""
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
from transformers import pipeline
|
13 |
+
|
14 |
+
from langchain import PromptTemplate
|
15 |
+
from langchain.document_loaders import WebBaseLoader
|
16 |
+
from langchain.schema import StrOutputParser
|
17 |
+
from langchain.schema.prompt_template import format_document
|
18 |
+
from langchain.chains import LLMChain
|
19 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
20 |
+
|
21 |
+
import ast
|
22 |
+
import os
|
23 |
+
import getpass
|
24 |
+
import matplotlib.pyplot as plt
|
25 |
+
|
26 |
+
api_key = os.environ.get('GOOGLE_API_KEY')
|
27 |
+
if api_key is None:
|
28 |
+
raise ValueError("No API key found. Please set the 'GOOGLE_API_KEY' environment variable.")
|
29 |
+
|
30 |
+
os.environ['GOOGLE_API_KEY'] = api_key
|
31 |
+
|
32 |
+
# Initialize the OCR pipeline
|
33 |
+
ocr_pipe = pipeline("image-to-text", model="jinhybr/OCR-Donut-CORD")
|
34 |
+
|
35 |
+
|
36 |
+
# Define the initial prompt for the LLM
|
37 |
+
initial_prompt = '''
|
38 |
+
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:
|
39 |
+
|
40 |
+
1. The type of test (General or Academic)
|
41 |
+
2. The task number (Task 1 or Task 2)
|
42 |
+
3. The specific question for this task
|
43 |
+
4. The author's written response
|
44 |
+
|
45 |
+
Please follow these steps for your evaluation:
|
46 |
+
|
47 |
+
1. Provide a score on the IELTS scale (0-9 points).
|
48 |
+
2. Leave a space.
|
49 |
+
3. Detail the weaknesses and mistakes in the writing.
|
50 |
+
|
51 |
+
Evaluate the writing based on the following four criteria:
|
52 |
+
- Task Response
|
53 |
+
- Coherence and Cohesion
|
54 |
+
- Lexical Resource
|
55 |
+
- Grammatical Range and Accuracy
|
56 |
+
|
57 |
+
Respond in this exact format:
|
58 |
+
|
59 |
+
Task type: [task type]
|
60 |
+
|
61 |
+
Task number: [task number]
|
62 |
+
|
63 |
+
score: [score]
|
64 |
+
|
65 |
+
Weaknesses and Mistakes:
|
66 |
+
|
67 |
+
[Weaknesses and Mistakes]
|
68 |
+
|
69 |
+
Notes:
|
70 |
+
|
71 |
+
- The text may have been obtained via OCR, which could result in some errors.
|
72 |
+
- Disregard any text enclosed in <>. They separate different parts of the text.
|
73 |
+
- 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.
|
74 |
+
- If the specific question is not present in the task content, mention that the question does not exist in the task response.
|
75 |
+
|
76 |
+
|
77 |
+
Task type: {task_type}
|
78 |
+
|
79 |
+
Task number: {task_number}
|
80 |
+
|
81 |
+
Question: {question}
|
82 |
+
|
83 |
+
Task content: {content}
|
84 |
+
'''
|
85 |
+
|
86 |
+
# Initialize the LLM
|
87 |
+
llm_model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.7, top_p=0.85)
|
88 |
+
|
89 |
+
# Define the prompt template
|
90 |
+
prompt = PromptTemplate(input_variables=['task_type', 'task_number', 'question', 'content'], template=initial_prompt)
|
91 |
+
|
92 |
+
# Define the LLM chain
|
93 |
+
chain = LLMChain(
|
94 |
+
llm=llm_model,
|
95 |
+
prompt=prompt,
|
96 |
+
)
|
97 |
+
|
98 |
+
def evaluate(task_type, task_number, question, image):
|
99 |
+
# Process the image to extract text
|
100 |
+
text_content = ocr_pipe(image)
|
101 |
+
content = text_content[0]['generated_text']
|
102 |
+
|
103 |
+
# Run the chain
|
104 |
+
result = chain.run({
|
105 |
+
'task_type': task_type,
|
106 |
+
'task_number': task_number,
|
107 |
+
'question': question,
|
108 |
+
'content': content
|
109 |
+
})
|
110 |
+
|
111 |
+
return result
|
112 |
+
|
113 |
+
# Create the Gradio interface
|
114 |
+
inputs = [
|
115 |
+
gr.Dropdown(choices=["Academic", "General"], label="Task Type", value="Academic"),
|
116 |
+
gr.Dropdown(choices=["Task 1", "Task 2"], label="Task Number", value="Task 1"),
|
117 |
+
gr.Textbox(label="Question", value=""),
|
118 |
+
gr.Image(type="pil", label="Upload Image")
|
119 |
+
]
|
120 |
+
|
121 |
+
|
122 |
+
outputs = gr.Markdown(label="Result")
|
123 |
+
|
124 |
+
gr.Interface(fn=evaluate, inputs=inputs, outputs=outputs, title="IELTS Writing Evaluation").launch()
|