File size: 1,462 Bytes
b9b337d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gradio as gr
import json
import os
from datetime import datetime
from pathlib import Path

gr.set_static_paths(paths=[Path.cwd().absolute()])
                    
print("Current Working Directory: ", os.getcwd())
cwd = os.getcwd()

def process_question(question):
    try:
        answer = {"doc1": "document 1 contents", "doc2": "document 2 contents"}
        # Create file in current directory with timestamp
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        filename = f"my_answer_{timestamp}.txt"# Get the current working directory
        filepath = os.path.join(cwd, filename)

        f = open(filename, "w", encoding="utf-8")
        try:
            answer = json.dumps(answer, indent=2, ensure_ascii=False)
            f.write(answer)
        finally:
            f.close()

        return [answer, filepath]
    
    except Exception as e:
        return f"Error processing question: {str(e)}", None

# Create Gradio interface
iface = gr.Interface(
    fn=process_question,
    inputs=gr.Textbox(label="Ask a question about documents", placeholder="Extract the text from this document."),
    outputs=[
        gr.Textbox(label="Answer"),
        gr.File(label="Download Answer as txt", file_count="single", file_types=[".txt"], type="filepath" )

    ],
    title="Document Reader",
    description="Ask questions about documents using AI vision to read scanned PDFs"
    )

iface.launch(ssr_mode=False, allowed_paths=[cwd])