Spaces:
Running
Running
import gradio as gr | |
from PIL import Image | |
import pytesseract | |
import openai | |
import os | |
# Ensure you have your OpenAI API key set as an environment variable | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
def extract_and_summarize(image): | |
# Extract text from image | |
text = pytesseract.image_to_string(image) | |
# Prepare the prompt for GPT-4 | |
prompt = f"Please summarize the following text:\n\n{text}" | |
# Call GPT-4 API for summarization | |
response = openai.ChatCompletion.create( | |
model="gpt-4", | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant."}, | |
{"role": "user", "content": prompt} | |
] | |
) | |
# Extract summary from GPT-4 response | |
summary = response['choices'][0]['message']['content'] | |
return summary | |
# Define Gradio interface | |
iface = gr.Interface( | |
fn=extract_and_summarize, | |
inputs=gr.Image(type="pil", label="Upload Document Image"), | |
outputs=gr.Textbox(label="Summarized Text"), | |
title="Document Summarizer", | |
description="Upload an image of a document and get a summarized text." | |
) | |
# Launch the interface | |
iface.launch() | |