Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
|
|
5 |
|
6 |
-
|
|
|
7 |
|
8 |
-
def
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
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=
|
53 |
-
inputs=gr.
|
54 |
-
outputs="
|
55 |
-
title="Document
|
56 |
-
description="Upload an image of a document and get a
|
57 |
)
|
58 |
|
59 |
-
|
|
|
|
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()
|