gauthamnairy commited on
Commit
385769a
·
verified ·
1 Parent(s): b163dc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -67
app.py CHANGED
@@ -1,67 +1,76 @@
1
- import gradio as gr
2
- import os
3
- from pageindex.core.tree_index import TreeIndex
4
- from llm_config import get_llm_client, get_model_name
5
-
6
- # Initialize clients (checking for environment variables)
7
- # We do this inside the function or globally, but for robustness inside function is safer if env vars change (less likely in HF Spaces)
8
- # However, initializing once is better for connection pooling if applicable. Let's do it inside for now to handle errors gracefully.
9
-
10
- def process_docling_and_chat(markdown_text, user_query):
11
- if not markdown_text:
12
- return "Please provide document markdown text."
13
- if not user_query:
14
- return "Please provide a query."
15
-
16
- try:
17
- # 1. Build the PageIndex Tree locally in the Space
18
- tree = TreeIndex()
19
- tree.build_from_markdown(markdown_text)
20
-
21
- # 2. Initialize the Navigator (The "Brain")
22
- # Try Nvidia first, then Mistral
23
- try:
24
- client = get_llm_client(provider="nvidia")
25
- model = get_model_name(provider="nvidia")
26
- # Test connection simply or just proceed
27
- except Exception as e:
28
- print(f"Nvidia client failed: {e}. Falling back to Mistral.")
29
- client = get_llm_client(provider="mistral")
30
- model = get_model_name(provider="mistral")
31
-
32
- # 3. Perform Reasoning Search
33
- # This uses the internal logic of the repo to navigate the tree
34
- context = tree.reasoning_search(query=user_query, llm_client=client)
35
-
36
- # 4. Final Answer Extraction
37
- # Using the same client for consistency
38
- response = client.chat.completions.create(
39
- model=model,
40
- messages=[
41
- {"role": "system", "content": "You are a helpful assistant. Use the provided context to answer the user's query."},
42
- {"role": "user", "content": f"Context:\n{context}\n\nQuery: {user_query}"}
43
- ]
44
- )
45
- return response.choices[0].message.content
46
-
47
- except Exception as e:
48
- return f"An error occurred: {str(e)}"
49
-
50
- # Gradio UI setup
51
- with gr.Blocks(title="Petromind AI - PageIndex RAG") as demo:
52
- gr.Markdown("# Oil & Gas Report - PageIndex RAG")
53
- gr.Markdown("Upload document content (markdown format) and ask questions to extract specific information using PageIndex reasoning.")
54
-
55
- with gr.Row():
56
- with gr.Column(scale=1):
57
- input_md = gr.Textbox(label="Paste Docling Markdown Here", lines=15, placeholder="# Document Title\n\n## Section 1\nContent...")
58
- with gr.Column(scale=1):
59
- query = gr.Textbox(label="What do you want to extract?", placeholder="e.g., What is the casing size?")
60
- btn = gr.Button("Analyze", variant="primary")
61
- output = gr.Textbox(label="Result", lines=10, interactive=False)
62
-
63
- btn.click(fn=process_docling_and_chat, inputs=[input_md, query], outputs=output)
64
-
65
- if __name__ == "__main__":
66
- # Enable queue for concurrency
67
- demo.queue().launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from pageindex.core.tree_index import TreeIndex
4
+ from llm_config import get_llm_client, get_model_name
5
+
6
+ # Initialize clients (checking for environment variables)
7
+ # We do this inside the function or globally, but for robustness inside function is safer if env vars change (less likely in HF Spaces)
8
+ # However, initializing once is better for connection pooling if applicable. Let's do it inside for now to handle errors gracefully.
9
+
10
+
11
+ # Security: Check for APP_TOKEN env var. If not set, default to open access or a warning.
12
+ # User provided specific token to use.
13
+ REQUIRED_TOKEN = os.getenv("APP_TOKEN", "849ejdkf2Audjo2Jf3jdoirfjh")
14
+
15
+ def process_docling_and_chat(markdown_text, user_query, token):
16
+ if token != REQUIRED_TOKEN:
17
+ return "Error: Invalid Authentication Token."
18
+
19
+ if not markdown_text:
20
+ return "Please provide document markdown text."
21
+ if not user_query:
22
+ return "Please provide a query."
23
+
24
+ try:
25
+ # 1. Build the PageIndex Tree locally in the Space
26
+ tree = TreeIndex()
27
+ tree.build_from_markdown(markdown_text)
28
+
29
+ # 2. Initialize the Navigator (The "Brain")
30
+ # Try Nvidia first, then Mistral
31
+ try:
32
+ client = get_llm_client(provider="nvidia")
33
+ model = get_model_name(provider="nvidia")
34
+ # Test connection simply or just proceed
35
+ except Exception as e:
36
+ print(f"Nvidia client failed: {e}. Falling back to Mistral.")
37
+ client = get_llm_client(provider="mistral")
38
+ model = get_model_name(provider="mistral")
39
+
40
+ # 3. Perform Reasoning Search
41
+ # This uses the internal logic of the repo to navigate the tree
42
+ context = tree.reasoning_search(query=user_query, llm_client=client)
43
+
44
+ # 4. Final Answer Extraction
45
+ # Using the same client for consistency
46
+ response = client.chat.completions.create(
47
+ model=model,
48
+ messages=[
49
+ {"role": "system", "content": "You are a helpful assistant. Use the provided context to answer the user's query."},
50
+ {"role": "user", "content": f"Context:\n{context}\n\nQuery: {user_query}"}
51
+ ]
52
+ )
53
+ return response.choices[0].message.content
54
+
55
+ except Exception as e:
56
+ return f"An error occurred: {str(e)}"
57
+
58
+ # Gradio UI setup
59
+ with gr.Blocks(title="Petromind AI - PageIndex RAG") as demo:
60
+ gr.Markdown("# Oil & Gas Report - PageIndex RAG")
61
+ gr.Markdown("Upload document content (markdown format) and ask questions to extract specific information using PageIndex reasoning.")
62
+
63
+ with gr.Row():
64
+ with gr.Column(scale=1):
65
+ input_md = gr.Textbox(label="Paste Docling Markdown Here", lines=15, placeholder="# Document Title\n\n## Section 1\nContent...")
66
+ with gr.Column(scale=1):
67
+ query = gr.Textbox(label="What do you want to extract?", placeholder="e.g., What is the casing size?")
68
+ token_input = gr.Textbox(label="API Token", placeholder="Enter access token", type="password")
69
+ btn = gr.Button("Analyze", variant="primary")
70
+ output = gr.Textbox(label="Result", lines=10, interactive=False)
71
+
72
+ btn.click(fn=process_docling_and_chat, inputs=[input_md, query, token_input], outputs=output)
73
+
74
+ if __name__ == "__main__":
75
+ # Enable queue for concurrency
76
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860)