JUNGU commited on
Commit
4f1688d
1 Parent(s): 6f5b327

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -51
app.py CHANGED
@@ -1,59 +1,41 @@
1
  import gradio as gr
2
- import requests
3
- import json
4
- import base64
 
5
 
6
- API_KEY = "your_api_key" # Replace with your actual API key
 
7
 
8
- def encode_image_to_base64(image):
9
- encoded_image = base64.b64encode(image).decode("utf-8")
10
- return encoded_image
11
-
12
- def extract_text_from_image(encoded_image):
13
- url = "https://api.openai.com/v1/images/generations"
14
- headers = {
15
- "Content-Type": "application/json",
16
- "Authorization": f"Bearer {API_KEY}",
17
- }
18
- data = {
19
- "prompt": "Extract the text from the document image.",
20
- "image": encoded_image,
21
- "n": 1,
22
- "size": "1024x1024",
23
- }
24
- response = requests.post(url, headers=headers, data=json.dumps(data))
25
- extracted_text = response.json()["data"][0]["text"]
26
- return extracted_text
27
-
28
- def summarize_text(text):
29
- url = "https://api.openai.com/v1/completions"
30
- headers = {
31
- "Content-Type": "application/json",
32
- "Authorization": f"Bearer {API_KEY}",
33
- }
34
- data = {
35
- "prompt": f"Summarize the following text:\n\n{text}\n\nSummary:",
36
- "max_tokens": 100,
37
- "n": 1,
38
- "stop": None,
39
- "temperature": 0.7,
40
- }
41
- response = requests.post(url, headers=headers, data=json.dumps(data))
42
- summary = response.json()["choices"][0]["text"]
43
- return summary
44
-
45
- def document_summarizer(image):
46
- encoded_image = encode_image_to_base64(image)
47
- extracted_text = extract_text_from_image(encoded_image)
48
- summary = summarize_text(extracted_text)
49
  return summary
50
 
 
51
  iface = gr.Interface(
52
- fn=document_summarizer,
53
- inputs=gr.inputs.Image(type="file", label="Upload Document Image"),
54
- outputs="text",
55
- title="Document Image Summarizer",
56
- description="Upload an image of a document and get a summary of its content.",
57
  )
58
 
59
- iface.launch()
 
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ import pytesseract
4
+ import openai
5
+ import os
6
 
7
+ # Ensure you have your OpenAI API key set as an environment variable
8
+ openai.api_key = os.getenv("OPENAI_API_KEY")
9
 
10
+ def extract_and_summarize(image):
11
+ # Extract text from image
12
+ text = pytesseract.image_to_string(image)
13
+
14
+ # Prepare the prompt for GPT-4
15
+ prompt = f"Please summarize the following text:\n\n{text}"
16
+
17
+ # Call GPT-4 API for summarization
18
+ response = openai.ChatCompletion.create(
19
+ model="gpt-4",
20
+ messages=[
21
+ {"role": "system", "content": "You are a helpful assistant."},
22
+ {"role": "user", "content": prompt}
23
+ ]
24
+ )
25
+
26
+ # Extract summary from GPT-4 response
27
+ summary = response['choices'][0]['message']['content']
28
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  return summary
30
 
31
+ # Define Gradio interface
32
  iface = gr.Interface(
33
+ fn=extract_and_summarize,
34
+ inputs=gr.Image(type="pil", label="Upload Document Image"),
35
+ outputs=gr.Textbox(label="Summarized Text"),
36
+ title="Document Summarizer",
37
+ description="Upload an image of a document and get a summarized text."
38
  )
39
 
40
+ # Launch the interface
41
+ iface.launch()