Spaces:
Running
Running
File size: 13,333 Bytes
ebd324a 00a711c ebd324a 00a711c ebd324a 00a711c ebd324a 00a711c ebd324a 00a711c 1ee72b8 f32bde2 1ee72b8 ebd324a 1ee72b8 ebd324a a130b03 ebd324a a130b03 ebd324a a130b03 ebd324a 9344768 ebd324a d6f1646 ebd324a 1ee72b8 ebd324a 00a711c ebd324a 00a711c ebd324a 1ee72b8 ebd324a e4c5b39 ebd324a 698e114 00a711c 698e114 ebd324a 698e114 |
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
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 detailed descriptions
description = {
("Academic", "Task 1"): """You are evaluating an Academic Task 1. The candidate is expected to describe, summarize, or explain a visual representation (such as a graph, chart, table, or diagram). They should write a minimum of 150 words. Key features to look for include:
1. **Introduction:** A brief introduction of what the visual representation is about.
2. **Overview:** A clear and concise summary of the main trends, differences, or stages without going into too much detail.
3. **Detailed Description:** Description of the main features in detail, highlighting significant trends, making comparisons where relevant, and mentioning important data points or changes.
4. **Logical Structure:** The report should be organized logically, ensuring it flows coherently from one point to the next.
5. **Formal Tone:** The writing should maintain a formal and objective tone, avoiding personal opinions or casual language.
6. **Accurate Data Reporting:** Accurate reporting of data without inaccuracies or overgeneralizations.
7. **Use of Linking Words:** Appropriate use of linking words and phrases to connect ideas and sentences (e.g., however, therefore, in contrast, similarly).""",
("General", "Task 1"): """You are evaluating a General Training Task 1. The candidate is expected to write a letter in response to a given situation. They should write a minimum of 150 words. Key features to look for include:
1. **Introduction:** A clear statement of the purpose of the letter in the opening sentence.
2. **Body:** Inclusion of relevant details depending on the task (e.g., explaining a problem, extending an invitation). If writing a complaint, they should explain the problem and possible solutions. If writing an invitation, include necessary details like date, time, and location.
3. **Conclusion:** A polite closing statement.
4. **Appropriate Tone:** Matching the tone of the letter to the given situation (formal for official letters, semi-formal for professional acquaintances, and informal for friends or family).
5. **Structure and Organization:** Organized into clear paragraphs, each addressing a specific point or aspect of the task.
6. **Polite and Courteous Language:** Polite and courteous language, especially in formal and semi-formal letters.
7. **Use of Linking Words:** Appropriate use of linking words and phrases to ensure coherence (e.g., firstly, moreover, consequently).""",
("Academic", "Task 2"): """You are evaluating an Academic Task 2. The candidate is expected to write an essay in response to a point of view, argument, or problem. They should write a minimum of 250 words. Key features to look for include:
1. **Introduction:** An introduction that paraphrases the task statement and states the thesis or main argument.
2. **Body Paragraphs:** Development of the argument in 2-3 body paragraphs, each focusing on a single main idea, supported by examples and evidence.
3. **Clear Position:** A clear and consistent position on the topic throughout the essay. If discussing both sides of an issue, making it clear which side they agree with (if any).
4. **Logical Progression:** Logical organization and coherence from one point to the next.
5. **Formal Tone:** Maintenance of a formal and academic tone throughout.
6. **Use of Linking Words:** Appropriate use of linking words and phrases to connect ideas and paragraphs (e.g., on the other hand, in addition, furthermore, in conclusion).
7. **Conclusion:** A conclusion that summarizes the main points and restates the position or provides a final thought.
8. **Complex Sentence Structures:** Demonstration of a variety of complex sentence structures.
9. **Vocabulary Range:** Use of a wide range of vocabulary accurately and appropriately.""",
("General", "Task 2"): """You are evaluating a General Training Task 2. The candidate is expected to write an essay in response to a point of view, argument, or problem. They should write a minimum of 250 words. Key features to look for include:
1. **Introduction:** An introduction that paraphrases the task statement and states the thesis or main argument.
2. **Body Paragraphs:** Development of the argument in 2-3 body paragraphs, each focusing on a single main idea, supported by examples and evidence.
3. **Clear Position:** A clear and consistent position on the topic throughout the essay. If discussing both sides of an issue, making it clear which side they agree with (if any).
4. **Logical Progression:** Logical organization and coherence from one point to the next.
5. **Formal Tone:** Maintenance of a formal and academic tone throughout.
6. **Use of Linking Words:** Appropriate use of linking words and phrases to connect ideas and paragraphs (e.g., on the other hand, in addition, furthermore, in conclusion).
7. **Conclusion:** A conclusion that summarizes the main points and restates the position or provides a final thought.
8. **Complex Sentence Structures:** Demonstration of a variety of complex sentence structures.
9. **Vocabulary Range:** Use of a wide range of vocabulary accurately and appropriately."""
}
# 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:
{description}
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 Achievement
- Coherence and Cohesion
- Lexical Resource (Vocabulary)
- Grammatical Range and Accuracy
- Overall Band Score
- Feedback and Additional Comments
- Strategies for Enhancement
Below is an example of how to respond. respond this way:
```
## Task type: General Training
## Task number: Task 2
## Overall score: 4.0
## Task Achievement:
- The candidate has partially addressed the given task.
- The essay presents a clear stance of disagreement with the statement that interviews are not a reliable method of choosing whom to employ.
- However, the ideas presented are not fully developed and lack specific examples or evidence to support the arguments.
- The essay does not adequately cover all aspects of the task, as it mainly focuses on the importance of interviews for high-level jobs and fails to discuss the potential drawbacks or other perspectives.
- Suggested Band Score (Task Achievement): 4.5
## Coherence and Cohesion:
- The essay lacks clarity and fluidity in transitions between sentences and paragraphs.
- Connecting words and phrases are not effectively used to maintain a smooth progression of ideas.
- The logical sequence and arrangement of information is not well-organized.
- The essay jumps between different topics without providing clear connections between them.
- Suggested Band Score (Coherence and Cohesion): 3.5
## Lexical Resource (Vocabulary):
- The essay demonstrates a limited range of vocabulary.
- There are several inaccuracies in vocabulary use, such as "relay their performance" and "ruins the operation."
- The vocabulary used is not always appropriate for the context and the level of formality required in academic writing.
- Suggested Band Score (Lexical Resource): 3.5
## Grammatical Range and Accuracy:
- The essay exhibits a limited variety of sentence structures.
- There are several grammatical errors, including incorrect verb forms ("qualify" instead of "qualified") and sentence construction ("For example, if an applicant copies the cv to online and submits it to the company without checking if he is qualified then ruins the operation").
- The use of punctuation is inconsistent and sometimes incorrect.
- Suggested Band Score (Grammatical Range and Accuracy): 3.5
## Overall Band Score:
- Considering the holistic performance across all criteria, the overall band score for this essay is 4.0.
- The essay fails to meet the requirements of the task adequately, lacks coherence and cohesion, and exhibits limited lexical resources and grammatical accuracy.
## Feedback and Additional Comments:
- **Strengths:**
- Clear stance on the topic
- **Areas for Improvement:**
- Develop arguments with specific examples and evidence
- Improve transitions and logical flow of ideas
- Expand vocabulary range and use more precise and appropriate language
- Correct grammatical errors and improve sentence construction
- Pay attention to punctuation and ensure consistency
## Strategies for Enhancement:
- Practice writing essays on various topics to improve vocabulary and sentence structure.
- Seek feedback from peers or a writing tutor to identify areas for improvement.
- Study grammar rules and practice exercises to enhance grammatical accuracy.
- Read widely to expose yourself to a wider range of vocabulary and writing styles.
```
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 question section is empty, try to identify the question in the task content.
- 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.4, top_p=0.85)
# Define the prompt template
prompt = PromptTemplate(input_variables=['task_type', 'task_number', 'question', 'content', 'description'], 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,
'description': description
})
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")
]
tracking_id = os.environ.get('TRACKING_ID')
if tracking_id is None:
raise ValueError("No tracking_id key found. Please set the 'tracking_id' environment variable.")
# Create the Gradio interface output
outputs = gr.Markdown(label="Result")
# Define the footer HTML with Font Awesome icons and Google Analytics
footer_html_with_analytics = """
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'YOUR_TRACKING_ID');
</script>
<!-- End Google Analytics -->
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<div style='text-align: center; margin-top: 20px;'>
<p>Developed by Hossein Mohseni</p>
<p>Contact Information:</p>
<p>
<a href='mailto:[email protected]' style='margin-right: 10px;'>
<i class='fas fa-envelope'></i>
</a>
<a href='https://www.linkedin.com/in/mohsenihossein/' target='_blank' style='margin-right: 10px;'>
<i class='fab fa-linkedin'></i>
</a>
<a href='https://t.me/mohsenih1999' target='_blank'>
<i class='fab fa-telegram'></i>
</a>
</p>
<p>This application is a demonstration. To enhance and improve it, your feedback is highly appreciated.</p>
</div>
"""
# Replace YOUR_TRACKING_ID with your actual Google Analytics tracking ID
footer_html_with_analytics = footer_html_with_analytics.replace("YOUR_TRACKING_ID", tracking_id)
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Interface(fn=evaluate, inputs=inputs, outputs=outputs, title="IELTS Writing Evaluation").launch(share=True, debug=True)
gr.HTML(footer_html_with_analytics)
# Launch the interface
demo.launch(share=True, debug=True)
|