abidlabs HF Staff commited on
Commit
b9b337d
·
verified ·
1 Parent(s): d28f7f9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import os
4
+ from datetime import datetime
5
+ from pathlib import Path
6
+
7
+ gr.set_static_paths(paths=[Path.cwd().absolute()])
8
+
9
+ print("Current Working Directory: ", os.getcwd())
10
+ cwd = os.getcwd()
11
+
12
+ def process_question(question):
13
+ try:
14
+ answer = {"doc1": "document 1 contents", "doc2": "document 2 contents"}
15
+ # Create file in current directory with timestamp
16
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
17
+ filename = f"my_answer_{timestamp}.txt"# Get the current working directory
18
+ filepath = os.path.join(cwd, filename)
19
+
20
+ f = open(filename, "w", encoding="utf-8")
21
+ try:
22
+ answer = json.dumps(answer, indent=2, ensure_ascii=False)
23
+ f.write(answer)
24
+ finally:
25
+ f.close()
26
+
27
+ return [answer, filepath]
28
+
29
+ except Exception as e:
30
+ return f"Error processing question: {str(e)}", None
31
+
32
+ # Create Gradio interface
33
+ iface = gr.Interface(
34
+ fn=process_question,
35
+ inputs=gr.Textbox(label="Ask a question about documents", placeholder="Extract the text from this document."),
36
+ outputs=[
37
+ gr.Textbox(label="Answer"),
38
+ gr.File(label="Download Answer as txt", file_count="single", file_types=[".txt"], type="filepath" )
39
+
40
+ ],
41
+ title="Document Reader",
42
+ description="Ask questions about documents using AI vision to read scanned PDFs"
43
+ )
44
+
45
+ iface.launch(ssr_mode=False, allowed_paths=[cwd])