hosseinhimself commited on
Commit
1ee72b8
1 Parent(s): e4c5b39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -17
app.py CHANGED
@@ -1,18 +1,9 @@
1
  import gradio as gr
2
-
3
  from transformers import pipeline
4
-
5
  from langchain import PromptTemplate
6
- from langchain.document_loaders import WebBaseLoader
7
- from langchain.schema import StrOutputParser
8
- from langchain.schema.prompt_template import format_document
9
  from langchain.chains import LLMChain
10
  from langchain_google_genai import ChatGoogleGenerativeAI
11
-
12
- import ast
13
  import os
14
- import getpass
15
- import matplotlib.pyplot as plt
16
 
17
  api_key = os.environ.get('GOOGLE_API_KEY')
18
  if api_key is None:
@@ -23,15 +14,58 @@ os.environ['GOOGLE_API_KEY'] = api_key
23
  # Initialize the OCR pipeline
24
  ocr_pipe = pipeline("image-to-text", model="jinhybr/OCR-Donut-CORD")
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  # Define the initial prompt for the LLM
28
  initial_prompt = '''
29
  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:
30
 
31
- 1. The type of test (General or Academic)
32
- 2. The task number (Task 1 or Task 2)
33
- 3. The specific question for this task
34
- 4. The author's written response
35
 
36
  Please follow these steps for your evaluation:
37
 
@@ -78,7 +112,7 @@ Task content: {content}
78
  llm_model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.7, top_p=0.85)
79
 
80
  # Define the prompt template
81
- prompt = PromptTemplate(input_variables=['task_type', 'task_number', 'question', 'content'], template=initial_prompt)
82
 
83
  # Define the LLM chain
84
  chain = LLMChain(
@@ -91,12 +125,16 @@ def evaluate(task_type, task_number, question, image):
91
  text_content = ocr_pipe(image)
92
  content = text_content[0]['generated_text']
93
 
 
 
 
94
  # Run the chain
95
  result = chain.run({
96
  'task_type': task_type,
97
  'task_number': task_number,
98
  'question': question,
99
- 'content': content
 
100
  })
101
 
102
  return result
@@ -109,7 +147,6 @@ inputs = [
109
  gr.Image(type="pil", label="Upload Image")
110
  ]
111
 
112
-
113
  outputs = gr.Markdown(label="Result")
114
 
115
- gr.Interface(fn=evaluate, inputs=inputs, outputs=outputs, title="IELTS Writing Evaluation").launch(share=True)
 
1
  import gradio as gr
 
2
  from transformers import pipeline
 
3
  from langchain import PromptTemplate
 
 
 
4
  from langchain.chains import LLMChain
5
  from langchain_google_genai import ChatGoogleGenerativeAI
 
 
6
  import os
 
 
7
 
8
  api_key = os.environ.get('GOOGLE_API_KEY')
9
  if api_key is None:
 
14
  # Initialize the OCR pipeline
15
  ocr_pipe = pipeline("image-to-text", model="jinhybr/OCR-Donut-CORD")
16
 
17
+ # Define detailed descriptions
18
+ descriptions = {
19
+ ("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:
20
+
21
+ 1. **Introduction:** A brief introduction of what the visual representation is about.
22
+ 2. **Overview:** A clear and concise summary of the main trends, differences, or stages without going into too much detail.
23
+ 3. **Detailed Description:** Description of the main features in detail, highlighting significant trends, making comparisons where relevant, and mentioning important data points or changes.
24
+ 4. **Logical Structure:** The report should be organized logically, ensuring it flows coherently from one point to the next.
25
+ 5. **Formal Tone:** The writing should maintain a formal and objective tone, avoiding personal opinions or casual language.
26
+ 6. **Accurate Data Reporting:** Accurate reporting of data without inaccuracies or overgeneralizations.
27
+ 7. **Use of Linking Words:** Appropriate use of linking words and phrases to connect ideas and sentences (e.g., however, therefore, in contrast, similarly).""",
28
+
29
+ ("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:
30
+
31
+ 1. **Introduction:** A clear statement of the purpose of the letter in the opening sentence.
32
+ 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.
33
+ 3. **Conclusion:** A polite closing statement.
34
+ 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).
35
+ 5. **Structure and Organization:** Organized into clear paragraphs, each addressing a specific point or aspect of the task.
36
+ 6. **Polite and Courteous Language:** Polite and courteous language, especially in formal and semi-formal letters.
37
+ 7. **Use of Linking Words:** Appropriate use of linking words and phrases to ensure coherence (e.g., firstly, moreover, consequently).""",
38
+
39
+ ("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:
40
+
41
+ 1. **Introduction:** An introduction that paraphrases the task statement and states the thesis or main argument.
42
+ 2. **Body Paragraphs:** Development of the argument in 2-3 body paragraphs, each focusing on a single main idea, supported by examples and evidence.
43
+ 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).
44
+ 4. **Logical Progression:** Logical organization and coherence from one point to the next.
45
+ 5. **Formal Tone:** Maintenance of a formal and academic tone throughout.
46
+ 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).
47
+ 7. **Conclusion:** A conclusion that summarizes the main points and restates the position or provides a final thought.
48
+ 8. **Complex Sentence Structures:** Demonstration of a variety of complex sentence structures.
49
+ 9. **Vocabulary Range:** Use of a wide range of vocabulary accurately and appropriately.""",
50
+
51
+ ("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:
52
+
53
+ 1. **Introduction:** An introduction that paraphrases the task statement and states the thesis or main argument.
54
+ 2. **Body Paragraphs:** Development of the argument in 2-3 body paragraphs, each focusing on a single main idea, supported by examples and evidence.
55
+ 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).
56
+ 4. **Logical Progression:** Logical organization and coherence from one point to the next.
57
+ 5. **Formal Tone:** Maintenance of a formal and academic tone throughout.
58
+ 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).
59
+ 7. **Conclusion:** A conclusion that summarizes the main points and restates the position or provides a final thought.
60
+ 8. **Complex Sentence Structures:** Demonstration of a variety of complex sentence structures.
61
+ 9. **Vocabulary Range:** Use of a wide range of vocabulary accurately and appropriately."""
62
+ }
63
 
64
  # Define the initial prompt for the LLM
65
  initial_prompt = '''
66
  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:
67
 
68
+ {description}
 
 
 
69
 
70
  Please follow these steps for your evaluation:
71
 
 
112
  llm_model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.7, top_p=0.85)
113
 
114
  # Define the prompt template
115
+ prompt = PromptTemplate(input_variables=['task_type', 'task_number', 'question', 'content', 'description'], template=initial_prompt)
116
 
117
  # Define the LLM chain
118
  chain = LLMChain(
 
125
  text_content = ocr_pipe(image)
126
  content = text_content[0]['generated_text']
127
 
128
+ # Select the appropriate description based on user input
129
+ description = descriptions.get((task_type, task_number), "")
130
+
131
  # Run the chain
132
  result = chain.run({
133
  'task_type': task_type,
134
  'task_number': task_number,
135
  'question': question,
136
+ 'content': content,
137
+ 'description': description
138
  })
139
 
140
  return result
 
147
  gr.Image(type="pil", label="Upload Image")
148
  ]
149
 
 
150
  outputs = gr.Markdown(label="Result")
151
 
152
+ gr.Interface(fn=evaluate, inputs=inputs, outputs=outputs, title="IELTS Writing Evaluation").launch(share=True)