hosseinhimself commited on
Commit
00a711c
·
verified ·
1 Parent(s): 9344768

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -1,9 +1,18 @@
1
  import gradio as gr
 
2
  from transformers import pipeline
3
- from langchain_core.prompts import PromptTemplate
4
- from langchain_core.runnables import RunnableSequence
 
 
 
 
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,6 +23,7 @@ os.environ['GOOGLE_API_KEY'] = api_key
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:
@@ -109,27 +119,24 @@ Task content: {content}
109
  '''
110
 
111
  # Initialize the LLM
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 RunnableSequence
118
- sequence = RunnableSequence([
119
- prompt,
120
- llm_model
121
- ])
122
 
123
  def evaluate(task_type, task_number, question, image):
124
  # Process the image to extract text
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 sequence
132
- result = sequence({
133
  'task_type': task_type,
134
  'task_number': task_number,
135
  'question': question,
@@ -147,6 +154,7 @@ inputs = [
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)
 
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
  # Initialize the OCR pipeline
24
  ocr_pipe = pipeline("image-to-text", model="jinhybr/OCR-Donut-CORD")
25
 
26
+
27
  # Define detailed descriptions
28
  descriptions = {
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:
 
119
  '''
120
 
121
  # Initialize the LLM
122
+ llm_model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.5, top_p=0.85)
123
 
124
  # Define the prompt template
125
  prompt = PromptTemplate(input_variables=['task_type', 'task_number', 'question', 'content', 'description'], template=initial_prompt)
126
 
127
+ # Define the LLM chain
128
+ chain = LLMChain(
129
+ llm=llm_model,
130
+ prompt=prompt,
131
+ )
132
 
133
  def evaluate(task_type, task_number, question, image):
134
  # Process the image to extract text
135
  text_content = ocr_pipe(image)
136
  content = text_content[0]['generated_text']
137
 
138
+ # Run the chain
139
+ result = chain.run({
 
 
 
140
  'task_type': task_type,
141
  'task_number': task_number,
142
  'question': question,
 
154
  gr.Image(type="pil", label="Upload Image")
155
  ]
156
 
157
+
158
  outputs = gr.Markdown(label="Result")
159
 
160
+ gr.Interface(fn=evaluate, inputs=inputs, outputs=outputs, title="IELTS Writing Evaluation").launch(share=True)