Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|