hosseinhimself commited on
Commit
6afe6e2
1 Parent(s): 332053a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -155
app.py CHANGED
@@ -1,163 +1,40 @@
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:
19
  raise ValueError("No API key found. Please set the 'GOOGLE_API_KEY' environment variable.")
20
 
21
- os.environ['GOOGLE_API_KEY'] = api_key
 
 
22
 
23
- # Initialize the OCR pipeline
24
- ocr_pipe = pipeline("image-to-text", model="jinhybr/OCR-Donut-CORD")
 
25
 
 
 
 
26
 
27
- # Define detailed descriptions
28
- description = {
29
- ("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:
30
 
31
- 1. **Introduction:** A brief introduction of what the visual representation is about.
32
- 2. **Overview:** A clear and concise summary of the main trends, differences, or stages without going into too much detail.
33
- 3. **Detailed Description:** Description of the main features in detail, highlighting significant trends, making comparisons where relevant, and mentioning important data points or changes.
34
- 4. **Logical Structure:** The report should be organized logically, ensuring it flows coherently from one point to the next.
35
- 5. **Formal Tone:** The writing should maintain a formal and objective tone, avoiding personal opinions or casual language.
36
- 6. **Accurate Data Reporting:** Accurate reporting of data without inaccuracies or overgeneralizations.
37
- 7. **Use of Linking Words:** Appropriate use of linking words and phrases to connect ideas and sentences (e.g., however, therefore, in contrast, similarly).""",
38
-
39
- ("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:
40
-
41
- 1. **Introduction:** A clear statement of the purpose of the letter in the opening sentence.
42
- 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.
43
- 3. **Conclusion:** A polite closing statement.
44
- 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).
45
- 5. **Structure and Organization:** Organized into clear paragraphs, each addressing a specific point or aspect of the task.
46
- 6. **Polite and Courteous Language:** Polite and courteous language, especially in formal and semi-formal letters.
47
- 7. **Use of Linking Words:** Appropriate use of linking words and phrases to ensure coherence (e.g., firstly, moreover, consequently).""",
48
-
49
- ("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:
50
-
51
- 1. **Introduction:** An introduction that paraphrases the task statement and states the thesis or main argument.
52
- 2. **Body Paragraphs:** Development of the argument in 2-3 body paragraphs, each focusing on a single main idea, supported by examples and evidence.
53
- 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).
54
- 4. **Logical Progression:** Logical organization and coherence from one point to the next.
55
- 5. **Formal Tone:** Maintenance of a formal and academic tone throughout.
56
- 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).
57
- 7. **Conclusion:** A conclusion that summarizes the main points and restates the position or provides a final thought.
58
- 8. **Complex Sentence Structures:** Demonstration of a variety of complex sentence structures.
59
- 9. **Vocabulary Range:** Use of a wide range of vocabulary accurately and appropriately.""",
60
-
61
- ("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:
62
-
63
- 1. **Introduction:** An introduction that paraphrases the task statement and states the thesis or main argument.
64
- 2. **Body Paragraphs:** Development of the argument in 2-3 body paragraphs, each focusing on a single main idea, supported by examples and evidence.
65
- 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).
66
- 4. **Logical Progression:** Logical organization and coherence from one point to the next.
67
- 5. **Formal Tone:** Maintenance of a formal and academic tone throughout.
68
- 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).
69
- 7. **Conclusion:** A conclusion that summarizes the main points and restates the position or provides a final thought.
70
- 8. **Complex Sentence Structures:** Demonstration of a variety of complex sentence structures.
71
- 9. **Vocabulary Range:** Use of a wide range of vocabulary accurately and appropriately."""
72
- }
73
-
74
- # Define the initial prompt for the LLM
75
- initial_prompt = '''
76
- 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:
77
- {description}
78
- Please follow these steps for your evaluation:
79
- 1. Provide a score on the IELTS scale (0-9 points).
80
- 2. Leave a space.
81
- 3. Detail the weaknesses and mistakes in the writing.
82
-
83
- Evaluate the writing based on the following four criteria:
84
- - Task Achievement
85
- - Coherence and Cohesion
86
- - Lexical Resource (Vocabulary)
87
- - Grammatical Range and Accuracy
88
- - Overall Band Score
89
- - Feedback and Additional Comments
90
- - Strategies for Enhancement
91
-
92
- Below is an example of how to respond. respond this way:
93
- ```
94
- ## Task type: General Training
95
-
96
- ## Task number: Task 2
97
-
98
- ## Overall score: 4.0
99
-
100
- ## Task Achievement:
101
- - The candidate has partially addressed the given task.
102
- - The essay presents a clear stance of disagreement with the statement that interviews are not a reliable method of choosing whom to employ.
103
- - However, the ideas presented are not fully developed and lack specific examples or evidence to support the arguments.
104
- - 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.
105
- - Suggested Band Score (Task Achievement): 4.5
106
-
107
- ## Coherence and Cohesion:
108
- - The essay lacks clarity and fluidity in transitions between sentences and paragraphs.
109
- - Connecting words and phrases are not effectively used to maintain a smooth progression of ideas.
110
- - The logical sequence and arrangement of information is not well-organized.
111
- - The essay jumps between different topics without providing clear connections between them.
112
- - Suggested Band Score (Coherence and Cohesion): 3.5
113
-
114
- ## Lexical Resource (Vocabulary):
115
- - The essay demonstrates a limited range of vocabulary.
116
- - There are several inaccuracies in vocabulary use, such as "relay their performance" and "ruins the operation."
117
- - The vocabulary used is not always appropriate for the context and the level of formality required in academic writing.
118
- - Suggested Band Score (Lexical Resource): 3.5
119
-
120
- ## Grammatical Range and Accuracy:
121
- - The essay exhibits a limited variety of sentence structures.
122
- - 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").
123
- - The use of punctuation is inconsistent and sometimes incorrect.
124
- - Suggested Band Score (Grammatical Range and Accuracy): 3.5
125
-
126
- ## Overall Band Score:
127
- - Considering the holistic performance across all criteria, the overall band score for this essay is 4.0.
128
- - The essay fails to meet the requirements of the task adequately, lacks coherence and cohesion, and exhibits limited lexical resources and grammatical accuracy.
129
-
130
- ## Feedback and Additional Comments:
131
- - **Strengths:**
132
- - Clear stance on the topic
133
-
134
- - **Areas for Improvement:**
135
- - Develop arguments with specific examples and evidence
136
- - Improve transitions and logical flow of ideas
137
- - Expand vocabulary range and use more precise and appropriate language
138
- - Correct grammatical errors and improve sentence construction
139
- - Pay attention to punctuation and ensure consistency
140
-
141
- ## Strategies for Enhancement:
142
- - Practice writing essays on various topics to improve vocabulary and sentence structure.
143
- - Seek feedback from peers or a writing tutor to identify areas for improvement.
144
- - Study grammar rules and practice exercises to enhance grammatical accuracy.
145
- - Read widely to expose yourself to a wider range of vocabulary and writing styles.
146
-
147
- ```
148
-
149
- Notes:
150
- - The text may have been obtained via OCR, which could result in some errors.
151
- - Disregard any text enclosed in <>. They separate different parts of the text.
152
- - 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.
153
- - If the question section is empty, try to identify the question in the task content.
154
- - If the specific question is not present in the task content, mention that the question does not exist in the task response.
155
-
156
- Task type: {task_type}
157
- Task number: {task_number}
158
- Question: {question}
159
- Task content: {content}
160
- '''
161
 
162
  # Initialize the LLM
163
  llm_model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.4, top_p=0.85)
@@ -184,7 +61,7 @@ def evaluate(task_type, task_number, question, image):
184
  'task_number': task_number,
185
  'question': question,
186
  'content': content,
187
- 'description': description
188
  })
189
 
190
  return result
@@ -197,19 +74,13 @@ inputs = [
197
  gr.Image(type="pil", label="Upload Image")
198
  ]
199
 
200
- # Ensure TRACKING_ID is set in the environment
201
- tracking_id = os.environ.get('TRACKING_ID')
202
- if tracking_id is None:
203
- raise ValueError("No tracking_id key found. Please set the 'tracking_id' environment variable.")
204
-
205
- # Replace YOUR_TRACKING_ID with the actual Google Analytics tracking ID
206
- footer_html_with_analytics = """
207
- <script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_ID"></script>
208
  <script>
209
  window.dataLayer = window.dataLayer || [];
210
- function gtag(){dataLayer.push(arguments);}
211
  gtag('js', new Date());
212
- gtag('config', 'YOUR_TRACKING_ID');
213
  </script>
214
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
215
  <div style='text-align: center; margin-top: 20px;'>
@@ -228,7 +99,7 @@ footer_html_with_analytics = """
228
  </p>
229
  <p>This application is a demonstration. To enhance and improve it, your feedback is highly appreciated.</p>
230
  </div>
231
- """.replace("YOUR_TRACKING_ID", tracking_id)
232
 
233
  outputs = gr.Markdown(label="Result")
234
 
@@ -239,4 +110,4 @@ with gr.Blocks() as demo:
239
  gr.HTML(footer_html_with_analytics)
240
 
241
  # Launch the interface
242
- demo.launch(share=True, debug=True)
 
1
  import gradio as gr
 
2
  from transformers import pipeline
 
3
  from langchain import PromptTemplate
4
  from langchain.document_loaders import WebBaseLoader
5
  from langchain.schema import StrOutputParser
6
  from langchain.schema.prompt_template import format_document
7
  from langchain.chains import LLMChain
8
  from langchain_google_genai import ChatGoogleGenerativeAI
 
 
9
  import os
10
+ from PIL import Image
11
+ import json
12
 
13
+ # Retrieve the API keys and other secrets from the environment
14
  api_key = os.environ.get('GOOGLE_API_KEY')
15
  if api_key is None:
16
  raise ValueError("No API key found. Please set the 'GOOGLE_API_KEY' environment variable.")
17
 
18
+ tracking_id = os.environ.get('TRACKING_ID')
19
+ if tracking_id is None:
20
+ raise ValueError("No tracking ID found. Please set the 'TRACKING_ID' environment variable.")
21
 
22
+ initial_prompt = os.environ.get('initial_prompt')
23
+ if initial_prompt is None:
24
+ raise ValueError("No initial prompt found. Please set the 'initial_prompt' environment variable.")
25
 
26
+ description_json = os.environ.get('description')
27
+ if description_json is None:
28
+ raise ValueError("No description found. Please set the 'description' environment variable.")
29
 
30
+ # Convert the description JSON string to a dictionary
31
+ description = json.loads(description_json)
 
32
 
33
+ # Set the API key for Google
34
+ os.environ['GOOGLE_API_KEY'] = api_key
35
+
36
+ # Initialize the OCR pipeline
37
+ ocr_pipe = pipeline("image-to-text", model="jinhybr/OCR-Donut-CORD")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Initialize the LLM
40
  llm_model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.4, top_p=0.85)
 
61
  'task_number': task_number,
62
  'question': question,
63
  'content': content,
64
+ 'description': description[(task_type, task_number)]
65
  })
66
 
67
  return result
 
74
  gr.Image(type="pil", label="Upload Image")
75
  ]
76
 
77
+ footer_html_with_analytics = f"""
78
+ <script async src="https://www.googletagmanager.com/gtag/js?id={tracking_id}"></script>
 
 
 
 
 
 
79
  <script>
80
  window.dataLayer = window.dataLayer || [];
81
+ function gtag(){{dataLayer.push(arguments);}}
82
  gtag('js', new Date());
83
+ gtag('config', '{tracking_id}');
84
  </script>
85
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
86
  <div style='text-align: center; margin-top: 20px;'>
 
99
  </p>
100
  <p>This application is a demonstration. To enhance and improve it, your feedback is highly appreciated.</p>
101
  </div>
102
+ """
103
 
104
  outputs = gr.Markdown(label="Result")
105
 
 
110
  gr.HTML(footer_html_with_analytics)
111
 
112
  # Launch the interface
113
+ demo.launch(share=True, debug=True)