gauthamnairy commited on
Commit
48ed553
·
verified ·
1 Parent(s): bacee7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -21
app.py CHANGED
@@ -1,5 +1,6 @@
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
 
@@ -12,41 +13,67 @@ from llm_config import get_llm_client, get_model_name
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, model=model)
 
 
 
 
 
 
 
 
 
 
 
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 Senior Petroleum Engineer assistant.
 
50
  Your goal is to extract precise technical data from the provided document context.
51
 
52
  **Guidelines:**
@@ -64,14 +91,33 @@ Your goal is to extract precise technical data from the provided document contex
64
  "data": [{"x_label": 0, "y_label": 10}, ...]
65
  }
66
  ```
67
- """},
68
- {"role": "user", "content": f"Context:\n{context}\n\nQuery: {user_query}\n\nIf requesting data, provide a Markdown Table."}
69
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  )
71
- return response.choices[0].message.content
 
 
 
 
 
 
72
 
73
  except Exception as e:
74
- return f"An error occurred: {str(e)}"
75
 
76
  # Gradio UI setup
77
  with gr.Blocks(title="Petromind AI - PageIndex RAG") as demo:
@@ -84,10 +130,11 @@ with gr.Blocks(title="Petromind AI - PageIndex RAG") as demo:
84
  with gr.Column(scale=1):
85
  query = gr.Textbox(label="What do you want to extract?", placeholder="e.g., What is the casing size?")
86
  token_input = gr.Textbox(label="API Token", placeholder="Enter access token", type="password")
 
87
  btn = gr.Button("Analyze", variant="primary")
88
  output = gr.Textbox(label="Result", lines=10, interactive=False)
89
 
90
- btn.click(fn=process_docling_and_chat, inputs=[input_md, query, token_input], outputs=output, api_name="process_docling_and_chat")
91
 
92
  if __name__ == "__main__":
93
  # Enable queue for concurrency
 
1
  import gradio as gr
2
  import os
3
+ import json
4
  from pageindex.core.tree_index import TreeIndex
5
  from llm_config import get_llm_client, get_model_name
6
 
 
13
  # User provided specific token to use.
14
  REQUIRED_TOKEN = os.getenv("APP_TOKEN", "849ejdkf2Audjo2Jf3jdoirfjh")
15
 
16
+ def process_docling_and_chat(markdown_text, user_query, token, chat_history_json=None):
17
  if token != REQUIRED_TOKEN:
18
+ yield "Error: Invalid Authentication Token."
19
+ return
20
 
21
  if not markdown_text:
22
+ yield "Please provide document markdown text."
23
+ return
24
  if not user_query:
25
+ yield "Please provide a query."
26
+ return
27
 
28
  try:
29
+ # History parsing
30
+ chat_history = []
31
+ if chat_history_json:
32
+ try:
33
+ chat_history = json.loads(chat_history_json)
34
+ except:
35
+ pass
36
+
37
+ reasoning_log = ""
38
+ yield "<<<STATUS: Initializing PageIndex...>>>"
39
+
40
  # 1. Build the PageIndex Tree locally in the Space
41
+ reasoning_log += "<<<STATUS: Building Index from Markdown...>>>\n"
42
+ yield reasoning_log
43
  tree = TreeIndex()
44
  tree.build_from_markdown(markdown_text)
45
 
46
  # 2. Initialize the Navigator (The "Brain")
 
47
  try:
48
  client = get_llm_client(provider="nvidia")
49
  model = get_model_name(provider="nvidia")
 
50
  except Exception as e:
51
  print(f"Nvidia client failed: {e}. Falling back to Mistral.")
52
  client = get_llm_client(provider="mistral")
53
  model = get_model_name(provider="mistral")
54
 
55
+ # 3. Perform Reasoning Search (Streamed)
56
+ context = ""
57
+ # Use stream method if available, else fallback
58
+ if hasattr(tree, 'reasoning_search_stream'):
59
+ for update in tree.reasoning_search_stream(user_query=user_query, llm_client=client, model=model):
60
+ if update.startswith("<<<STATUS:"):
61
+ reasoning_log += update + "\n"
62
+ yield reasoning_log
63
+ else:
64
+ context = update # The last item is the full context
65
+ else:
66
+ reasoning_log += "<<<STATUS: Standard Reasoning Search...>>>\n"
67
+ yield reasoning_log
68
+ context = tree.reasoning_search(query=user_query, llm_client=client, model=model)
69
 
70
+ # 4. Final Answer Generation
71
+ reasoning_log += "<<<STATUS: Generating Final Answer...>>>\n"
72
+ yield reasoning_log
73
+
74
+ # Construct messages with history
75
+ messages = [
76
+ {"role": "system", "content": """You are a Senior Petroleum Engineer assistant.
77
  Your goal is to extract precise technical data from the provided document context.
78
 
79
  **Guidelines:**
 
91
  "data": [{"x_label": 0, "y_label": 10}, ...]
92
  }
93
  ```
94
+ """}
95
+ ]
96
+
97
+ # Add history
98
+ for msg in chat_history:
99
+ role = msg.get("role", "user")
100
+ content = msg.get("content", "")
101
+ messages.append({"role": role, "content": content})
102
+
103
+ messages.append({"role": "user", "content": f"Context:\n{context}\n\nQuery: {user_query}\n\nIf requesting data, provide a Markdown Table."})
104
+
105
+ response_stream = client.chat.completions.create(
106
+ model=model,
107
+ messages=messages,
108
+ stream=True,
109
+ max_tokens=8192
110
  )
111
+
112
+ full_response_text = ""
113
+ for chunk in response_stream:
114
+ if chunk.choices[0].delta.content:
115
+ delta = chunk.choices[0].delta.content
116
+ full_response_text += delta
117
+ yield reasoning_log + "\n" + full_response_text
118
 
119
  except Exception as e:
120
+ yield f"An error occurred: {str(e)}"
121
 
122
  # Gradio UI setup
123
  with gr.Blocks(title="Petromind AI - PageIndex RAG") as demo:
 
130
  with gr.Column(scale=1):
131
  query = gr.Textbox(label="What do you want to extract?", placeholder="e.g., What is the casing size?")
132
  token_input = gr.Textbox(label="API Token", placeholder="Enter access token", type="password")
133
+ history_json = gr.Textbox(visible=False, label="History JSON")
134
  btn = gr.Button("Analyze", variant="primary")
135
  output = gr.Textbox(label="Result", lines=10, interactive=False)
136
 
137
+ btn.click(fn=process_docling_and_chat, inputs=[input_md, query, token_input, history_json], outputs=output, api_name="process_docling_and_chat")
138
 
139
  if __name__ == "__main__":
140
  # Enable queue for concurrency