Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,35 @@
|
|
1 |
import easyocr
|
2 |
import gradio as gr
|
3 |
-
|
|
|
4 |
|
5 |
-
#
|
|
|
|
|
|
|
6 |
reader = easyocr.Reader(['ch_sim', 'en'])
|
7 |
-
gpt = pipeline('text-generation', model='gpt2')
|
8 |
|
9 |
# Define the OCR function
|
10 |
-
|
|
|
|
|
11 |
ocr_result = reader.readtext(image)
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
return corrected_text, gpt_opinion
|
15 |
|
16 |
# Define Gradio UI components
|
17 |
image_input = gr.inputs.Image()
|
18 |
-
gpt_input = gr.inputs.Textbox(lines=1, label="GPT
|
|
|
19 |
ocr_output = gr.outputs.Textbox(label="OCR Result (GPT Corrected)")
|
20 |
gpt_opinion_output = gr.outputs.Textbox(label="GPT Opinion on Image Information")
|
21 |
|
|
|
|
|
22 |
# Create Gradio interface
|
23 |
-
iface = gr.Interface(fn=ocr_gpt, inputs=[gpt_input,
|
24 |
#iface.launch(share=True)
|
|
|
1 |
import easyocr
|
2 |
import gradio as gr
|
3 |
+
import openai
|
4 |
+
import requests
|
5 |
|
6 |
+
# Set OpenAI API key
|
7 |
+
openai.api_key = "your_api_key_here"
|
8 |
+
|
9 |
+
# Initialize OCR reader
|
10 |
reader = easyocr.Reader(['ch_sim', 'en'])
|
|
|
11 |
|
12 |
# Define the OCR function
|
13 |
+
# Update the ocr_gpt function to accept the API key as an input
|
14 |
+
def ocr_gpt(image, gpt_opinion_prompt, api_key):
|
15 |
+
openai.api_key = api_key
|
16 |
ocr_result = reader.readtext(image)
|
17 |
+
prompt = "Correct the following OCR result: " + ocr_result[0][1]
|
18 |
+
response = openai.Completion.create(engine="davinci-codex", prompt=prompt, max_tokens=50, n=1, stop=None, temperature=0.5)
|
19 |
+
corrected_text = response.choices[0].text.strip()
|
20 |
+
gpt_opinion_response = openai.Completion.create(engine="davinci-codex", prompt=gpt_opinion_prompt, max_tokens=50, n=1, stop=None, temperature=0.5)
|
21 |
+
gpt_opinion = gpt_opinion_response.choices[0].text.strip()
|
22 |
return corrected_text, gpt_opinion
|
23 |
|
24 |
# Define Gradio UI components
|
25 |
image_input = gr.inputs.Image()
|
26 |
+
gpt_input = gr.inputs.Textbox(lines=1, label="GPT Opinion Prompt")
|
27 |
+
api_key_input = gr.inputs.Textbox(lines=1, label="API Key")
|
28 |
ocr_output = gr.outputs.Textbox(label="OCR Result (GPT Corrected)")
|
29 |
gpt_opinion_output = gr.outputs.Textbox(label="GPT Opinion on Image Information")
|
30 |
|
31 |
+
|
32 |
+
|
33 |
# Create Gradio interface
|
34 |
+
iface = gr.Interface(fn=ocr_gpt, inputs=[image_input, gpt_input, api_key_input], outputs=[ocr_output, gpt_opinion_output])
|
35 |
#iface.launch(share=True)
|