hosseinhimself commited on
Commit
0d737d9
1 Parent(s): 6afe6e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -14
app.py CHANGED
@@ -1,9 +1,6 @@
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
@@ -45,15 +42,23 @@ prompt = PromptTemplate(input_variables=['task_type', 'task_number', 'question',
45
  # Define the LLM chain
46
  chain = LLMChain(llm=llm_model, prompt=prompt)
47
 
48
- def evaluate(task_type, task_number, question, image):
49
- # Ensure the image is in the correct format
50
- if isinstance(image, str):
51
- # Load the image if it's a URL or path
52
- image = Image.open(image)
53
-
54
- # Process the image to extract text
55
- text_content = ocr_pipe(image)
56
- content = text_content[0]['generated_text']
 
 
 
 
 
 
 
 
57
 
58
  # Run the chain
59
  result = chain.run({
@@ -61,7 +66,7 @@ def evaluate(task_type, task_number, question, image):
61
  'task_number': task_number,
62
  'question': question,
63
  'content': content,
64
- 'description': description[(task_type, task_number)]
65
  })
66
 
67
  return result
@@ -71,9 +76,17 @@ inputs = [
71
  gr.Dropdown(choices=["Academic", "General"], label="Test Type", value="Academic"),
72
  gr.Dropdown(choices=["Task 1", "Task 2"], label="Task Number", value="Task 1"),
73
  gr.Textbox(label="Question", value=""),
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>
@@ -106,6 +119,12 @@ outputs = gr.Markdown(label="Result")
106
  # Define the Gradio Blocks and Interface
107
  with gr.Blocks() as demo:
108
  gr.Markdown("# IELTS Writing Evaluation")
 
 
 
 
 
 
109
  gr.Interface(fn=evaluate, inputs=inputs, outputs=outputs)
110
  gr.HTML(footer_html_with_analytics)
111
 
 
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
 
42
  # Define the LLM chain
43
  chain = LLMChain(llm=llm_model, prompt=prompt)
44
 
45
+ def evaluate(task_type, task_number, question, input_type, image=None, text=None):
46
+ if input_type == "Image" and image is not None:
47
+ # Ensure the image is in the correct format
48
+ if isinstance(image, str):
49
+ # Load the image if it's a URL or path
50
+ image = Image.open(image)
51
+
52
+ # Process the image to extract text
53
+ text_content = ocr_pipe(image)
54
+ content = text_content[0]['generated_text']
55
+ elif input_type == "Text" and text is not None:
56
+ content = text
57
+ else:
58
+ return "Please provide the required input based on your selection."
59
+
60
+ # Retrieve the description for the given task type and number, or use a default value
61
+ task_description = description.get((task_type, task_number), "No description available for this task.")
62
 
63
  # Run the chain
64
  result = chain.run({
 
66
  'task_number': task_number,
67
  'question': question,
68
  'content': content,
69
+ 'description': task_description
70
  })
71
 
72
  return result
 
76
  gr.Dropdown(choices=["Academic", "General"], label="Test Type", value="Academic"),
77
  gr.Dropdown(choices=["Task 1", "Task 2"], label="Task Number", value="Task 1"),
78
  gr.Textbox(label="Question", value=""),
79
+ gr.Radio(choices=["Image", "Text"], label="Input Type", value="Image", id="input_type"),
80
+ gr.Image(type="pil", label="Upload Image", id="image_input", visible=True),
81
+ gr.Textbox(label="Enter Text", id="text_input", visible=False)
82
  ]
83
 
84
+ def toggle_input(input_type):
85
+ if input_type == "Image":
86
+ return gr.update(visible=True), gr.update(visible=False)
87
+ else:
88
+ return gr.update(visible=False), gr.update(visible=True)
89
+
90
  footer_html_with_analytics = f"""
91
  <script async src="https://www.googletagmanager.com/gtag/js?id={tracking_id}"></script>
92
  <script>
 
119
  # Define the Gradio Blocks and Interface
120
  with gr.Blocks() as demo:
121
  gr.Markdown("# IELTS Writing Evaluation")
122
+ with gr.Row():
123
+ with gr.Column():
124
+ input_type_radio = gr.Radio(choices=["Image", "Text"], label="Input Type", value="Image")
125
+ image_input = gr.Image(type="pil", label="Upload Image", visible=True)
126
+ text_input = gr.Textbox(label="Enter Text", visible=False)
127
+ input_type_radio.change(toggle_input, input_type_radio, [image_input, text_input])
128
  gr.Interface(fn=evaluate, inputs=inputs, outputs=outputs)
129
  gr.HTML(footer_html_with_analytics)
130