File size: 1,155 Bytes
6f5b327
4f1688d
 
 
 
6f5b327
4f1688d
 
6f5b327
4f1688d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f5b327
 
4f1688d
6f5b327
4f1688d
 
 
 
 
6f5b327
 
4f1688d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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()