|
import easyocr |
|
import gradio as gr |
|
import openai |
|
import requests |
|
|
|
|
|
openai.api_key = "your_api_key_here" |
|
|
|
|
|
reader = easyocr.Reader(['ch_sim', 'en'],gpu=False) |
|
|
|
|
|
|
|
def ocr_gpt(image, gpt_opinion_prompt, api_key): |
|
openai.api_key = api_key |
|
ocr_result = reader.readtext(image) |
|
prompt = "Correct the following OCR result: " + ocr_result[0][1] |
|
response = openai.Completion.create(engine="davinci-codex", prompt=prompt, max_tokens=50, n=1, stop=None, temperature=0.5) |
|
corrected_text = response.choices[0].text.strip() |
|
gpt_opinion_response = openai.Completion.create(engine="davinci-codex", prompt=gpt_opinion_prompt, max_tokens=50, n=1, stop=None, temperature=0.5) |
|
gpt_opinion = gpt_opinion_response.choices[0].text.strip() |
|
return corrected_text, gpt_opinion |
|
|
|
|
|
image_input = gr.inputs.Image() |
|
gpt_input = gr.inputs.Textbox(lines=1, label="GPT Opinion Prompt") |
|
api_key_input = gr.inputs.Textbox(lines=1, label="API Key") |
|
ocr_output = gr.outputs.Textbox(label="OCR Result (GPT Corrected)") |
|
gpt_opinion_output = gr.outputs.Textbox(label="GPT Opinion on Image Information") |
|
|
|
|
|
|
|
|
|
iface = gr.Interface(fn=ocr_gpt, inputs=[image_input, gpt_input, api_key_input], outputs=[ocr_output, gpt_opinion_output]) |