JUNGU commited on
Commit
6f5b327
1 Parent(s): 80c7374

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()