Spaces:
Runtime error
Runtime error
Denver Citizen9
commited on
Commit
·
d701c7d
1
Parent(s):
b04c40f
updated - how big of an example?
Browse files
app.py
CHANGED
@@ -1,5 +1,85 @@
|
|
1 |
import gradio as gr
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
def process_json(json_data):
|
4 |
# Function implementation to process the JSON data
|
5 |
# You can replace this with your actual logic
|
@@ -2724,21 +2804,35 @@ def launch_demo():
|
|
2724 |
]
|
2725 |
}"""
|
2726 |
|
2727 |
-
|
|
|
|
|
|
|
2728 |
|
2729 |
-
|
2730 |
-
result = process_json(json_data)
|
2731 |
-
return result
|
2732 |
|
2733 |
-
gr.
|
2734 |
-
|
2735 |
-
|
2736 |
-
|
2737 |
-
|
2738 |
-
|
2739 |
-
|
2740 |
-
|
2741 |
-
|
2742 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2743 |
|
2744 |
-
launch_demo()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
|
4 |
+
def generate_text(openAI_key,prompt, engine="text-davinci-003"):
|
5 |
+
openai.api_key = openAI_key
|
6 |
+
completions = openai.Completion.create(
|
7 |
+
engine=engine,
|
8 |
+
prompt=prompt,
|
9 |
+
max_tokens=512,
|
10 |
+
n=1,
|
11 |
+
stop=None,
|
12 |
+
temperature=0.6,
|
13 |
+
)
|
14 |
+
message = completions.choices[0].text
|
15 |
+
return message
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
def munch_mail ():
|
20 |
+
# from generate_answer
|
21 |
+
#topn_chunks = recommender(question)
|
22 |
+
prompt = ""
|
23 |
+
|
24 |
+
prompt = """
|
25 |
+
You are an executive assistant. Your job is to review mail to identify the contents, categorize how important the contents are, and to succintly summarize the most relevant information.
|
26 |
+
|
27 |
+
All mail is sorted into a CATEGORY which is one of the following:
|
28 |
+
1) Important
|
29 |
+
2) Semi-Important
|
30 |
+
3) Not-Imporant
|
31 |
+
|
32 |
+
For documents that are of CATEGORY: Important, provide a short SUMMARY of the document and be sure to highlight and describe why the document is important. The summary should be in as few words as possible to convey the most critical information which is due dates, payment amounts, and help the user quickly understand why action is necessary. Be brief and succinct. Important mail includes things such as:
|
33 |
+
'''
|
34 |
+
Urgent personal correspondence: Cards, or other mail from friends or family
|
35 |
+
Legal documents: Such as court summons, subpoenas, or notices related to legal matters.
|
36 |
+
Official government correspondence: Including tax notices, passport renewal reminders, or social security updates.
|
37 |
+
Employment-related letters: Job offers, contracts, or important work-related communication.
|
38 |
+
'''
|
39 |
+
|
40 |
+
For documents that are of CATEGORY: Semi-Important, provide a short SUMMARY describing the contents. Examples of semi-important documents include things such as:
|
41 |
+
'''
|
42 |
+
Financial statements: Bank statements, credit card bills, or mortgage statements.
|
43 |
+
Health-related correspondence: Health insurance updates, appointment reminders, or test results.
|
44 |
+
Educational letters: College acceptance letters, course registration information, or academic progress reports.
|
45 |
+
'''
|
46 |
+
|
47 |
+
Examples of documents that are of CATEGORY: Not-Important include:
|
48 |
+
'''
|
49 |
+
Advertising mail: Promotional offers, catalogs, or coupons from various companies.
|
50 |
+
Non-urgent personal correspondence: Greeting cards, party invitations, or personal letters.
|
51 |
+
General informational mail: Community newsletters, local event announcements, or flyers.
|
52 |
+
'''
|
53 |
+
Also be aware that some Not-Important junk mail may be intentionally misleading, because it is trying to trick the recipient into believing that the document is more important than it actually is.
|
54 |
+
|
55 |
+
For all documents, come up with a short TITLE to describe what the document is but do not include information about the sender. Separately, identify the SENDER so we know where this document pertains to. Also include the name of the INPUT mail document that was provided. Also include an EXPLANATION for why this piece of mail was given this particular category classification. Also, provide a TINY_SUMMARY which is less than 15 words long short summary of the SUMMARY which is the most important parts.
|
56 |
+
|
57 |
+
Provide your response in JSON format as follows. Be sure to return only valid json:
|
58 |
+
|
59 |
+
{
|
60 |
+
"input": "<INPUT>",
|
61 |
+
"sender: "<SENDER>",
|
62 |
+
"title": "<TITLE>",
|
63 |
+
"summary": "<SUMMARY>",
|
64 |
+
"tinySummary": "<TINY_SUMMARY>",
|
65 |
+
"category": "<CATEGORY>",
|
66 |
+
"explanation": "<EXPLANATION>"
|
67 |
+
}
|
68 |
+
|
69 |
+
BELOW YOU WILL FIND THE CONTENTS OF THE MAIL, STARTING WITH THE INPUT NAME AND THEN THE CONTENTS
|
70 |
+
|
71 |
+
"""
|
72 |
+
|
73 |
+
#prompt += 'search results:\n\n'
|
74 |
+
#for c in topn_chunks:
|
75 |
+
# prompt += c + '\n\n'
|
76 |
+
|
77 |
+
# prompt += f"Query: {question}\nAnswer:"
|
78 |
+
|
79 |
+
openAI_key = os.getenv('my_openAI_key')
|
80 |
+
munched_mail = generate_text(openAI_key, prompt,"text-davinci-003")
|
81 |
+
return munched_mail
|
82 |
+
|
83 |
def process_json(json_data):
|
84 |
# Function implementation to process the JSON data
|
85 |
# You can replace this with your actual logic
|
|
|
2804 |
]
|
2805 |
}"""
|
2806 |
|
2807 |
+
title = 'Snail Mail Muncher'
|
2808 |
+
description = """Enter JSON data from PP OCR v3 and click Submit to process it.
|
2809 |
+
Note that an enhanced workflow would start with an image file, send it to a hosted PP-OCR inference API, process it to get this JSON, and submit for you. This is a simple example that let's you copy/paste json from your own document OR you may also use the example, which comes from the 'SampleCableBill.png' in the related repo. In this example we use OpenAI but with an updated prompt something open source like Dolly would be more ideal."""
|
2810 |
+
|
2811 |
|
2812 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
|
2813 |
|
2814 |
+
gr.Markdown(f'<center><h1>{title}</h1></center>')
|
2815 |
+
gr.Markdown(description)
|
2816 |
+
|
2817 |
+
with gr.Row():
|
2818 |
+
|
2819 |
+
with gr.Group():
|
2820 |
+
#openAI_key = os.getenv('my_openAI_key')
|
2821 |
+
gr.Markdown('Step 1: Use provided example, or ask PP-OCRv3 to read the text from your mail: https://www.paddlepaddle.org.cn/hub/scene/ocr')
|
2822 |
+
question = gr.Textbox(label='Step 2: The PP-OCRv3 JSON for your document goes here', default=default_example))
|
2823 |
+
#file = gr.File(label='Upload a sample piece of mail in PDF form', file_types=['.pdf'])
|
2824 |
+
#gr.Markdown("<center>* * * OR * * *</center>")
|
2825 |
+
#url = gr.Textbox(label='Alternate Approach: If more convenient, you can instead provide a link for your file here')
|
2826 |
+
gr.Markdown("<br>")
|
2827 |
+
#gr.Markdown('Step 2: Ask a specific question about your document')
|
2828 |
+
# added a default question to avoid errors
|
2829 |
+
|
2830 |
+
gr.Markdown("<br>")
|
2831 |
+
btn = gr.Button(value='Submit & Munch Mail')
|
2832 |
+
btn.style(full_width=True)
|
2833 |
+
gr.Markdown("<br>")
|
2834 |
+
#gr.Markdown('Step 3: Review generated response')
|
2835 |
+
munched_mail = gr.Textbox(label='The JSON for your PDF summary is :')
|
2836 |
+
btn.click(submit_json, inputs=[json_data], outputs=[munched_mail])
|
2837 |
+
demo.launch()
|
2838 |
|
|