Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Updated UI
Browse files
app.py
CHANGED
@@ -1,33 +1,750 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
# Create the Gradio interface using gr.Blocks
|
27 |
-
# Blocks gives us more control over the layout
|
28 |
-
with gr.Blocks() as demo:
|
29 |
-
# Use gr.Markdown to display the HTML content
|
30 |
-
gr.Markdown(banner_content)
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import logging
|
4 |
+
import asyncio
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
from uuid import uuid4
|
8 |
+
from datetime import datetime, timedelta
|
9 |
+
from pathlib import Path
|
10 |
+
from huggingface_hub import CommitScheduler, HfApi
|
11 |
+
from auditqa.sample_questions import QUESTIONS
|
12 |
+
from auditqa.reports import files, report_list, new_files, new_report_list
|
13 |
+
from auditqa.process_chunks import load_chunks, getconfig, get_local_qdrant
|
14 |
+
from auditqa.retriever import get_context
|
15 |
+
from auditqa.reader import nvidia_client, dedicated_endpoint, serverless_api, inf_provider
|
16 |
+
from auditqa.utils import make_html_source, parse_output_llm_with_sources, save_logs, get_message_template, get_client_location, get_client_ip, get_platform_info
|
17 |
+
from dotenv import load_dotenv
|
18 |
+
load_dotenv()
|
19 |
+
from threading import Lock
|
20 |
+
from gradio.routes import Request
|
21 |
+
import json
|
22 |
+
#import platform
|
23 |
+
#print(platform.python_version())
|
24 |
|
25 |
+
# fetch tokens and model config params
|
26 |
+
SPACES_LOG = os.environ["SPACES_LOG"]
|
27 |
+
#audit_space = os.environ["audit_space"]
|
28 |
+
model_config = getconfig("model_params.cfg")
|
29 |
+
|
30 |
+
# create the local logs repo
|
31 |
+
JSON_DATASET_DIR = Path("json_dataset")
|
32 |
+
JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
|
33 |
+
JSON_DATASET_PATH = JSON_DATASET_DIR / f"logs-{uuid4()}.json"
|
34 |
+
|
35 |
+
# the logs are written to dataset repo periodically from local logs
|
36 |
+
# https://huggingface.co/spaces/Wauplin/space_to_dataset_saver
|
37 |
+
scheduler = CommitScheduler(
|
38 |
+
repo_id="GIZ/spaces_logs",
|
39 |
+
repo_type="dataset",
|
40 |
+
folder_path=JSON_DATASET_DIR,
|
41 |
+
path_in_repo="eudr_chatbot", # Changed for EUDR
|
42 |
+
token=SPACES_LOG )
|
43 |
+
|
44 |
+
#####--------------- VECTOR STORE -------------------------------------------------
|
45 |
+
# reports contain the already created chunks from Markdown version of pdf reports
|
46 |
+
# document processing was done using : https://github.com/axa-group/Parsr
|
47 |
+
# We need to create the local vectorstore collection once using load_chunks
|
48 |
+
# vectorestore colection are stored on persistent storage so this needs to be run only once
|
49 |
+
# hence, comment out line below when creating for first time
|
50 |
+
#vectorstores = load_chunks()
|
51 |
+
# once the vectore embeddings are created we will use qdrant client to access these
|
52 |
+
#try:
|
53 |
+
vectorstores = get_local_qdrant()
|
54 |
+
#except Exception as e:
|
55 |
+
# api = HfApi()
|
56 |
+
# api.restart_space(repo_id = "GIZ/eudr_assistant", token=audit_space) # Changed for EUDR
|
57 |
+
|
58 |
+
#####---------------------CHAT-----------------------------------------------------
|
59 |
+
def start_chat(query,history):
|
60 |
+
history = history + [(query,None)]
|
61 |
+
history = [tuple(x) for x in history]
|
62 |
+
return (gr.update(interactive = False),gr.update(selected=1),history)
|
63 |
+
|
64 |
+
def finish_chat():
|
65 |
+
return (gr.update(interactive = True,value = ""))
|
66 |
+
|
67 |
+
def submit_feedback(feedback, logs_data):
|
68 |
+
"""Handle feedback submission"""
|
69 |
+
try:
|
70 |
+
if logs_data is None:
|
71 |
+
return gr.update(visible=False), gr.update(visible=True)
|
72 |
+
|
73 |
+
session_id = logs_data.get("session_id")
|
74 |
+
if session_id:
|
75 |
+
# Update session last_activity to now
|
76 |
+
session_manager.update_session(session_id)
|
77 |
+
# Compute duration from the session manager and update the log.
|
78 |
+
logs_data["session_duration_seconds"] = session_manager.get_session_duration(session_id)
|
79 |
+
|
80 |
+
# Now save the (feedback) log record
|
81 |
+
save_logs(scheduler, JSON_DATASET_PATH, logs_data, feedback)
|
82 |
+
return gr.update(visible=False), gr.update(visible=True)
|
83 |
+
except Exception as e:
|
84 |
+
return gr.update(visible=False), gr.update(visible=True)
|
85 |
+
|
86 |
+
def handle_geojson_upload(file):
|
87 |
+
"""Handle GeoJSON file upload"""
|
88 |
+
if file is not None:
|
89 |
+
return "✅ Document submitted successfully! You can now ask questions about this GeoJSON file."
|
90 |
+
else:
|
91 |
+
return "❌ Please select a GeoJSON file to upload."
|
92 |
+
|
93 |
+
# Session Manager added (track session duration, location, and platform)
|
94 |
+
class SessionManager:
|
95 |
+
def __init__(self):
|
96 |
+
self.sessions = {}
|
97 |
+
|
98 |
+
def create_session(self, client_ip, user_agent):
|
99 |
+
session_id = str(uuid4())
|
100 |
+
self.sessions[session_id] = {
|
101 |
+
'start_time': datetime.now(),
|
102 |
+
'last_activity': datetime.now(),
|
103 |
+
'client_ip': client_ip,
|
104 |
+
'location_info': get_client_location(client_ip),
|
105 |
+
'platform_info': get_platform_info(user_agent)
|
106 |
+
}
|
107 |
+
return session_id
|
108 |
+
|
109 |
+
def update_session(self, session_id):
|
110 |
+
if session_id in self.sessions:
|
111 |
+
self.sessions[session_id]['last_activity'] = datetime.now()
|
112 |
+
|
113 |
+
def get_session_duration(self, session_id):
|
114 |
+
if session_id in self.sessions:
|
115 |
+
start = self.sessions[session_id]['start_time']
|
116 |
+
last = self.sessions[session_id]['last_activity']
|
117 |
+
return (last - start).total_seconds()
|
118 |
+
return 0
|
119 |
+
|
120 |
+
def get_session_data(self, session_id):
|
121 |
+
return self.sessions.get(session_id)
|
122 |
+
|
123 |
+
# Initialize session manager
|
124 |
+
session_manager = SessionManager()
|
125 |
+
|
126 |
+
async def chat(query, history, method, country, uploaded_file, client_ip=None, session_id=None, request:gr.Request = None):
|
127 |
+
"""taking a query and a message history, use a pipeline (reformulation, retriever, answering)
|
128 |
+
to yield a tuple of:(messages in gradio format/messages in langchain format, source documents)
|
129 |
+
"""
|
130 |
+
|
131 |
+
if not session_id:
|
132 |
+
user_agent = request.headers.get('User-Agent','') if request else ''
|
133 |
+
session_id = session_manager.create_session(client_ip, user_agent)
|
134 |
+
else:
|
135 |
+
session_manager.update_session(session_id)
|
136 |
+
|
137 |
+
# Get session id
|
138 |
+
session_data = session_manager.get_session_data(session_id)
|
139 |
+
session_duration = session_manager.get_session_duration(session_id)
|
140 |
+
|
141 |
+
print(f">> NEW QUESTION : {query}")
|
142 |
+
print(f"history:{history}")
|
143 |
+
print(f"method:{method}")
|
144 |
+
print(f"country:{country}")
|
145 |
+
print(f"uploaded_file:{uploaded_file}")
|
146 |
+
|
147 |
+
docs_html = ""
|
148 |
+
output_query = ""
|
149 |
+
|
150 |
+
# Handle different methods
|
151 |
+
if method == "Upload GeoJSON":
|
152 |
+
if uploaded_file is None:
|
153 |
+
warning_message = "⚠️ **No GeoJSON file uploaded.** Please upload a GeoJSON file first."
|
154 |
+
history[-1] = (query, warning_message)
|
155 |
+
# Update logs with the warning instead of answer
|
156 |
+
logs_data = {
|
157 |
+
"record_id": str(uuid4()),
|
158 |
+
"session_id": session_id,
|
159 |
+
"session_duration_seconds": session_duration,
|
160 |
+
"client_location": session_data['location_info'],
|
161 |
+
"platform": session_data['platform_info'],
|
162 |
+
"question": query,
|
163 |
+
"method": method,
|
164 |
+
"uploaded_file": str(uploaded_file) if uploaded_file else None,
|
165 |
+
"retriever": model_config.get('retriever','MODEL'),
|
166 |
+
"endpoint_type": model_config.get('reader','TYPE'),
|
167 |
+
"reader": model_config.get('reader','NVIDIA_MODEL'),
|
168 |
+
"answer": warning_message,
|
169 |
+
"no_results": True # Flag to indicate no results were found
|
170 |
+
}
|
171 |
+
yield [tuple(x) for x in history], "", logs_data, session_id
|
172 |
+
# Save log for the warning response
|
173 |
+
save_logs(scheduler, JSON_DATASET_PATH, logs_data)
|
174 |
+
return
|
175 |
+
else: # "Talk to Reports"
|
176 |
+
if not country:
|
177 |
+
warning_message = "⚠️ **No country selected.** Please select a country to analyze reports."
|
178 |
+
history[-1] = (query, warning_message)
|
179 |
+
# Update logs with the warning instead of answer
|
180 |
+
logs_data = {
|
181 |
+
"record_id": str(uuid4()),
|
182 |
+
"session_id": session_id,
|
183 |
+
"session_duration_seconds": session_duration,
|
184 |
+
"client_location": session_data['location_info'],
|
185 |
+
"platform": session_data['platform_info'],
|
186 |
+
"question": query,
|
187 |
+
"method": method,
|
188 |
+
"country": country,
|
189 |
+
"retriever": model_config.get('retriever','MODEL'),
|
190 |
+
"endpoint_type": model_config.get('reader','TYPE'),
|
191 |
+
"reader": model_config.get('reader','NVIDIA_MODEL'),
|
192 |
+
"answer": warning_message,
|
193 |
+
"no_results": True # Flag to indicate no results were found
|
194 |
+
}
|
195 |
+
yield [tuple(x) for x in history], "", logs_data, session_id
|
196 |
+
# Save log for the warning response
|
197 |
+
save_logs(scheduler, JSON_DATASET_PATH, logs_data)
|
198 |
+
return
|
199 |
+
|
200 |
+
##------------------------fetch collection from vectorstore------------------------------
|
201 |
+
vectorstore = vectorstores["docling"]
|
202 |
+
|
203 |
+
##------------------------------get context----------------------------------------------
|
204 |
+
|
205 |
+
### adding for assessing computation time
|
206 |
+
start_time = time.time()
|
207 |
+
|
208 |
+
# Modified context retrieval for EUDR use case
|
209 |
+
if method == "Upload GeoJSON":
|
210 |
+
# For GeoJSON, use the uploaded file as context
|
211 |
+
# This is a placeholder - you'll need to implement GeoJSON processing
|
212 |
+
context_retrieved = [] # Implement GeoJSON processing here
|
213 |
+
else: # "Talk to Reports"
|
214 |
+
# For reports, filter by country
|
215 |
+
context_retrieved = get_context(vectorstore=vectorstore, query=query,
|
216 |
+
country=country) # Modified to use country instead of reports/sources
|
217 |
+
|
218 |
+
end_time = time.time()
|
219 |
+
print("Time for retriever:",end_time - start_time)
|
220 |
+
|
221 |
+
if not context_retrieved or len(context_retrieved) == 0:
|
222 |
+
if method == "Upload GeoJSON":
|
223 |
+
warning_message = "⚠️ **No relevant information found in the uploaded GeoJSON file.** Please try rephrasing your question or check your file."
|
224 |
+
else:
|
225 |
+
warning_message = f"⚠️ **No relevant information found for {country}.** Please try rephrasing your question or selecting a different country."
|
226 |
+
|
227 |
+
history[-1] = (query, warning_message)
|
228 |
+
# Update logs with the warning instead of answer
|
229 |
+
logs_data = {
|
230 |
+
"record_id": str(uuid4()),
|
231 |
+
"session_id": session_id,
|
232 |
+
"session_duration_seconds": session_duration,
|
233 |
+
"client_location": session_data['location_info'],
|
234 |
+
"platform": session_data['platform_info'],
|
235 |
+
"question": query,
|
236 |
+
"method": method,
|
237 |
+
"country": country if method == "Talk to Reports" else None,
|
238 |
+
"uploaded_file": str(uploaded_file) if method == "Upload GeoJSON" else None,
|
239 |
+
"retriever": model_config.get('retriever','MODEL'),
|
240 |
+
"endpoint_type": model_config.get('reader','TYPE'),
|
241 |
+
"reader": model_config.get('reader','NVIDIA_MODEL'),
|
242 |
+
"answer": warning_message,
|
243 |
+
"no_results": True # Flag to indicate no results were found
|
244 |
+
}
|
245 |
+
yield [tuple(x) for x in history], "", logs_data, session_id
|
246 |
+
# Save log for the warning response
|
247 |
+
save_logs(scheduler, JSON_DATASET_PATH, logs_data)
|
248 |
+
return
|
249 |
+
|
250 |
+
context_retrieved_formatted = "||".join(doc.page_content for doc in context_retrieved)
|
251 |
+
context_retrieved_lst = [doc.page_content for doc in context_retrieved]
|
252 |
+
|
253 |
+
##------------------- -------------Define Prompt-------------------------------------------
|
254 |
+
SYSTEM_PROMPT = """
|
255 |
+
You are EUDR Q&A, an AI Assistant for EU Deforestation Regulation analysis. \
|
256 |
+
You are given a question and extracted passages related to EUDR compliance and deforestation data.\
|
257 |
+
Provide a clear and structured answer based on the passages/context provided and the guidelines.
|
258 |
+
Guidelines:
|
259 |
+
- Passages are provided as comma separated list of strings
|
260 |
+
- If the passages have useful facts or numbers, use them in your answer.
|
261 |
+
- When you use information from a passage, mention where it came from by using [Doc i] at the end of the sentence. i stands for the number of the document.
|
262 |
+
- Do not use the sentence 'Doc i says ...' to say where information came from.
|
263 |
+
- If the same thing is said in more than one document, you can mention all of them like this: [Doc i, Doc j, Doc k]
|
264 |
+
- Do not just summarize each passage one by one. Group your summaries to highlight the key parts in the explanation.
|
265 |
+
- If it makes sense, use bullet points and lists to make your answers easier to understand.
|
266 |
+
- You do not need to use every passage. Only use the ones that help answer the question.
|
267 |
+
- If the documents do not have the information needed to answer the question, just say you do not have enough information.
|
268 |
+
"""
|
269 |
+
|
270 |
+
USER_PROMPT = """Passages:
|
271 |
+
{context}
|
272 |
+
-----------------------
|
273 |
+
Question: {question} - Explained for EUDR compliance expert
|
274 |
+
Answer in english with the passages citations:
|
275 |
+
""".format(context = context_retrieved_lst, question=query)
|
276 |
+
|
277 |
+
##-------------------- apply message template ------------------------------
|
278 |
+
messages = get_message_template(model_config.get('reader','TYPE'),SYSTEM_PROMPT,USER_PROMPT)
|
279 |
+
|
280 |
+
## -----------------Prepare HTML for displaying source documents --------------
|
281 |
+
docs_html = []
|
282 |
+
for i, d in enumerate(context_retrieved, 1):
|
283 |
+
docs_html.append(make_html_source(d, i))
|
284 |
+
docs_html = "".join(docs_html)
|
285 |
+
|
286 |
+
##-----------------------get answer from endpoints------------------------------
|
287 |
+
answer_yet = ""
|
288 |
+
|
289 |
+
logs_data = {
|
290 |
+
"record_id": str(uuid4()), # Add unique record ID
|
291 |
+
"session_id": session_id,
|
292 |
+
"session_duration_seconds": session_duration,
|
293 |
+
"client_location": session_data['location_info'],
|
294 |
+
"platform": session_data['platform_info'],
|
295 |
+
"system_prompt": SYSTEM_PROMPT,
|
296 |
+
"method": method,
|
297 |
+
"country": country if method == "Talk to Reports" else None,
|
298 |
+
"uploaded_file": str(uploaded_file) if method == "Upload GeoJSON" else None,
|
299 |
+
"question": query,
|
300 |
+
"retriever": model_config.get('retriever','MODEL'),
|
301 |
+
"endpoint_type": model_config.get('reader','TYPE'),
|
302 |
+
"reader": model_config.get('reader','NVIDIA_MODEL'),
|
303 |
+
"docs": [doc.page_content for doc in context_retrieved],
|
304 |
+
}
|
305 |
+
|
306 |
+
# Keep the same endpoint logic as the original...
|
307 |
+
if model_config.get('reader','TYPE') == 'NVIDIA':
|
308 |
+
chat_model = nvidia_client()
|
309 |
+
async def process_stream():
|
310 |
+
nonlocal answer_yet # Use the outer scope's answer_yet variable
|
311 |
+
response = chat_model.chat_completion(
|
312 |
+
model=model_config.get("reader","NVIDIA_MODEL"),
|
313 |
+
messages=messages,
|
314 |
+
stream=True,
|
315 |
+
max_tokens=int(model_config.get('reader','MAX_TOKENS')),
|
316 |
+
)
|
317 |
+
for message in response:
|
318 |
+
token = message.choices[0].delta.content
|
319 |
+
if token:
|
320 |
+
answer_yet += token
|
321 |
+
parsed_answer = parse_output_llm_with_sources(answer_yet)
|
322 |
+
history[-1] = (query, parsed_answer)
|
323 |
+
logs_data["answer"] = parsed_answer
|
324 |
+
yield [tuple(x) for x in history], docs_html, logs_data, session_id
|
325 |
+
|
326 |
+
# Stream the response updates
|
327 |
+
async for update in process_stream():
|
328 |
+
yield update
|
329 |
+
|
330 |
+
elif model_config.get('reader','TYPE') == 'INF_PROVIDERS':
|
331 |
+
chat_model = inf_provider()
|
332 |
+
start_time = time.time()
|
333 |
+
ai_prefix = "**AI-Generated Response:**\n\n"
|
334 |
+
async def process_stream():
|
335 |
+
nonlocal answer_yet
|
336 |
+
answer_yet += ai_prefix
|
337 |
+
response = chat_model.chat.completions.create(
|
338 |
+
model=model_config.get("reader","INF_PROVIDER_MODEL"),
|
339 |
+
messages = messages,
|
340 |
+
stream= True,
|
341 |
+
max_tokens=int(model_config.get('reader','MAX_TOKENS')),
|
342 |
+
)
|
343 |
+
for message in response:
|
344 |
+
token = message.choices[0].delta.content
|
345 |
+
if token:
|
346 |
+
answer_yet += token
|
347 |
+
parsed_answer = parse_output_llm_with_sources(answer_yet)
|
348 |
+
history[-1] = (query, parsed_answer)
|
349 |
+
logs_data["answer"] = parsed_answer
|
350 |
+
yield [tuple(x) for x in history], docs_html, logs_data, session_id
|
351 |
+
await asyncio.sleep(0.05)
|
352 |
+
|
353 |
+
# Stream the response updates
|
354 |
+
async for update in process_stream():
|
355 |
+
yield update
|
356 |
+
|
357 |
+
|
358 |
+
elif model_config.get('reader','TYPE') == 'DEDICATED':
|
359 |
+
chat_model = dedicated_endpoint()
|
360 |
+
### adding for assessing computation time
|
361 |
+
start_time = time.time()
|
362 |
+
async def process_stream():
|
363 |
+
nonlocal answer_yet # Use the outer scope's answer_yet variable
|
364 |
+
# Iterate over the streaming response chunks
|
365 |
+
async for chunk in chat_model.astream(messages):
|
366 |
+
token = chunk.content
|
367 |
+
answer_yet += token
|
368 |
+
parsed_answer = parse_output_llm_with_sources(answer_yet)
|
369 |
+
history[-1] = (query, parsed_answer)
|
370 |
+
logs_data["answer"] = parsed_answer
|
371 |
+
yield [tuple(x) for x in history], docs_html, logs_data, session_id
|
372 |
+
end_time = time.time()
|
373 |
+
print("Time for reader:",end_time - start_time)
|
374 |
+
|
375 |
+
# Stream the response updates
|
376 |
+
async for update in process_stream():
|
377 |
+
yield update
|
378 |
+
|
379 |
+
|
380 |
+
else:
|
381 |
+
chat_model = serverless_api() # TESTING: ADAPTED FOR HF INFERENCE API (needs to be reverted for production version)
|
382 |
+
async def process_stream():
|
383 |
+
nonlocal answer_yet
|
384 |
+
try:
|
385 |
+
formatted_messages = [
|
386 |
+
{
|
387 |
+
"role": msg.type if hasattr(msg, 'type') else msg.role,
|
388 |
+
"content": msg.content
|
389 |
+
}
|
390 |
+
for msg in messages
|
391 |
+
]
|
392 |
+
|
393 |
+
response = chat_model.chat_completion(
|
394 |
+
messages=formatted_messages,
|
395 |
+
max_tokens= int(model_config.get('reader', 'MAX_TOKENS'))
|
396 |
+
)
|
397 |
+
|
398 |
+
response_text = response.choices[0].message.content
|
399 |
+
words = response_text.split()
|
400 |
+
for word in words:
|
401 |
+
answer_yet += word + " "
|
402 |
+
parsed_answer = parse_output_llm_with_sources(answer_yet)
|
403 |
+
history[-1] = (query, parsed_answer)
|
404 |
+
# Update logs_data with current answer (and get a new timestamp)
|
405 |
+
logs_data["answer"] = parsed_answer
|
406 |
+
yield [tuple(x) for x in history], docs_html, logs_data, session_id
|
407 |
+
await asyncio.sleep(0.05)
|
408 |
+
|
409 |
+
except Exception as e:
|
410 |
+
raise
|
411 |
+
|
412 |
+
async for update in process_stream():
|
413 |
+
yield update
|
414 |
+
|
415 |
+
|
416 |
+
# logging the event
|
417 |
+
try:
|
418 |
+
save_logs(scheduler,JSON_DATASET_PATH,logs_data)
|
419 |
+
except Exception as e:
|
420 |
+
raise
|
421 |
+
|
422 |
+
|
423 |
+
#####-------------------------- Gradio App--------------------------------------####
|
424 |
+
|
425 |
+
# Set up Gradio Theme
|
426 |
+
theme = gr.themes.Base(
|
427 |
+
primary_hue="blue",
|
428 |
+
secondary_hue="red",
|
429 |
+
font=[gr.themes.GoogleFont("Poppins"), "ui-sans-serif", "system-ui", "sans-serif"],
|
430 |
+
text_size = gr.themes.utils.sizes.text_sm,
|
431 |
+
)
|
432 |
+
|
433 |
+
init_prompt = """
|
434 |
+
Hello, I am EUDR Q&A, an AI-powered conversational assistant designed to help you understand EU Deforestation Regulation compliance and analysis. I will answer your questions by using **EUDR reports and uploaded GeoJSON files**.
|
435 |
+
💡 How to use (tabs on right)
|
436 |
+
- **Data Sources**: You can choose to either upload a GeoJSON file for analysis or talk to EUDR reports filtered by country.
|
437 |
+
- **Examples**: We have curated some example questions, select a particular question from category of questions.
|
438 |
+
- **Sources**: This tab will display the relied upon content from the reports or uploaded files, to help you in assessing or fact checking if the answer provided by EUDR Q&A assistant is correct or not.
|
439 |
+
⚠️ For limitations of the tool and collection of usage statistics and data please check **Disclaimer** tab.
|
440 |
+
⚠️ By using this app, you acknowledge that we collect usage statistics (such as question asked, feedback given, session duration, device type and anonymized geo-location) to understand performance and continuously improve the tool, based on our legitimate interest in enhancing our services.
|
441 |
"""
|
442 |
|
|
|
|
|
|
|
|
|
|
|
443 |
|
444 |
+
with gr.Blocks(title="EUDR Q&A", css= "style.css", theme=theme,elem_id = "main-component") as demo:
|
445 |
+
#----------------------------------------------------------------------------------------------
|
446 |
+
# main tab where chat interaction happens
|
447 |
+
# ---------------------------------------------------------------------------------------------
|
448 |
+
with gr.Tab("EUDR Q&A"):
|
449 |
+
|
450 |
+
with gr.Row(elem_id="chatbot-row"):
|
451 |
+
# chatbot output screen
|
452 |
+
with gr.Column(scale=2):
|
453 |
+
chatbot = gr.Chatbot(
|
454 |
+
value=[(None,init_prompt)],
|
455 |
+
show_copy_button=True,show_label = False,elem_id="chatbot",layout = "panel",
|
456 |
+
avatar_images = (None,"data-collection.png"),
|
457 |
+
)
|
458 |
+
|
459 |
+
# feedback UI
|
460 |
+
with gr.Column(elem_id="feedback-container"):
|
461 |
+
with gr.Row(visible=False) as feedback_row:
|
462 |
+
gr.Markdown("Was this response helpful?")
|
463 |
+
with gr.Row():
|
464 |
+
okay_btn = gr.Button("👍 Okay", elem_classes="feedback-button")
|
465 |
+
not_okay_btn = gr.Button("👎 Not to expectations", elem_classes="feedback-button")
|
466 |
+
feedback_thanks = gr.Markdown("Thanks for the feedback!", visible=False)
|
467 |
+
feedback_state = gr.State()
|
468 |
+
|
469 |
+
with gr.Row(elem_id = "input-message"):
|
470 |
+
textbox=gr.Textbox(placeholder="Ask me anything here!",show_label=False,scale=7,
|
471 |
+
lines = 1,interactive = True,elem_id="input-textbox")
|
472 |
+
|
473 |
+
# second column with playground area for user to select values
|
474 |
+
with gr.Column(scale=1, variant="panel",elem_id = "right-panel"):
|
475 |
+
# creating tabs on right panel
|
476 |
+
with gr.Tabs() as tabs:
|
477 |
+
#---------------- tab for DATA SOURCES SELECTION ----------------------
|
478 |
+
|
479 |
+
with gr.Tab("Data Sources",elem_id = "tab-config",id = 2):
|
480 |
+
|
481 |
+
#---------------- SELECTION METHOD - RADIO BUTTON ------------
|
482 |
+
search_method = gr.Radio(
|
483 |
+
choices=["Upload GeoJSON", "Talk to Reports"],
|
484 |
+
label="Choose data source",
|
485 |
+
info= "Upload a GeoJSON file for analysis or select country-specific EUDR reports",
|
486 |
+
value="Upload GeoJSON",
|
487 |
+
)
|
488 |
+
|
489 |
+
#---------------- UPLOAD GEOJSON SECTION ------------
|
490 |
+
with gr.Group(visible=True) as geojson_section:
|
491 |
+
uploaded_file = gr.File(
|
492 |
+
label="Upload GeoJSON File",
|
493 |
+
file_types=[".geojson", ".json"],
|
494 |
+
file_count="single"
|
495 |
+
)
|
496 |
+
|
497 |
+
upload_status = gr.Markdown("", visible=False)
|
498 |
+
|
499 |
+
submit_file_btn = gr.Button("Submit GeoJSON", variant="primary")
|
500 |
+
|
501 |
+
#---------------- TALK TO REPORTS SECTION ------------
|
502 |
+
with gr.Group(visible=False) as reports_section:
|
503 |
+
#----- Country filter ----------
|
504 |
+
dropdown_country = gr.Dropdown(
|
505 |
+
["Ecuador", "Guatemala"],
|
506 |
+
label="Select Country",
|
507 |
+
value=None,
|
508 |
+
interactive=True,
|
509 |
+
)
|
510 |
+
|
511 |
+
# Toggle visibility based on search method
|
512 |
+
def toggle_search_method(method):
|
513 |
+
"""Toggle between GeoJSON upload and country selection"""
|
514 |
+
if method == "Upload GeoJSON":
|
515 |
+
return (
|
516 |
+
gr.update(visible=True), # geojson_section
|
517 |
+
gr.update(visible=False), # reports_section
|
518 |
+
gr.update(value=None), # dropdown_country
|
519 |
+
gr.update(value=None) # uploaded_file
|
520 |
+
)
|
521 |
+
else: # "Talk to Reports"
|
522 |
+
return (
|
523 |
+
gr.update(visible=False), # geojson_section
|
524 |
+
gr.update(visible=True), # reports_section
|
525 |
+
gr.update(), # dropdown_country
|
526 |
+
gr.update(value=None) # uploaded_file
|
527 |
+
)
|
528 |
+
|
529 |
+
# Pass to the event handler
|
530 |
+
search_method.change(
|
531 |
+
fn=toggle_search_method,
|
532 |
+
inputs=[search_method],
|
533 |
+
outputs=[
|
534 |
+
geojson_section,
|
535 |
+
reports_section,
|
536 |
+
dropdown_country,
|
537 |
+
uploaded_file
|
538 |
+
]
|
539 |
+
)
|
540 |
+
|
541 |
+
# Handle file upload submission
|
542 |
+
submit_file_btn.click(
|
543 |
+
fn=handle_geojson_upload,
|
544 |
+
inputs=[uploaded_file],
|
545 |
+
outputs=[upload_status]
|
546 |
+
).then(
|
547 |
+
lambda: gr.update(visible=True),
|
548 |
+
outputs=[upload_status]
|
549 |
+
)
|
550 |
+
|
551 |
+
|
552 |
+
############### tab for Question selection ###############
|
553 |
+
with gr.TabItem("Examples",elem_id = "tab-examples",id = 0):
|
554 |
+
examples_hidden = gr.Textbox(visible = False)
|
555 |
+
|
556 |
+
# getting defualt key value to display
|
557 |
+
first_key = list(QUESTIONS.keys())[0]
|
558 |
+
# create the question category dropdown
|
559 |
+
dropdown_samples = gr.Dropdown(QUESTIONS.keys(),value = first_key,
|
560 |
+
interactive = True,show_label = True,
|
561 |
+
label = "Select a category of sample questions",
|
562 |
+
elem_id = "dropdown-samples")
|
563 |
+
|
564 |
+
|
565 |
+
# iterate through the questions list
|
566 |
+
samples = []
|
567 |
+
for i,key in enumerate(QUESTIONS.keys()):
|
568 |
+
examples_visible = True if i == 0 else False
|
569 |
+
with gr.Row(visible = examples_visible) as group_examples:
|
570 |
+
examples_questions = gr.Examples(
|
571 |
+
QUESTIONS[key],
|
572 |
+
[examples_hidden],
|
573 |
+
examples_per_page=8,
|
574 |
+
run_on_click=False,
|
575 |
+
elem_id=f"examples{i}",
|
576 |
+
api_name=f"examples{i}",
|
577 |
+
)
|
578 |
+
|
579 |
+
samples.append(group_examples)
|
580 |
+
##------------------- tab for Sources reporting ##------------------
|
581 |
+
with gr.Tab("Sources",elem_id = "tab-citations",id = 1):
|
582 |
+
sources_textbox = gr.HTML(show_label=False, elem_id="sources-textbox")
|
583 |
+
docs_textbox = gr.State("")
|
584 |
+
|
585 |
+
def change_sample_questions(key):
|
586 |
+
# update the questions list based on key selected
|
587 |
+
index = list(QUESTIONS.keys()).index(key)
|
588 |
+
visible_bools = [False] * len(samples)
|
589 |
+
visible_bools[index] = True
|
590 |
+
return [gr.update(visible=visible_bools[i]) for i in range(len(samples))]
|
591 |
+
|
592 |
+
dropdown_samples.change(change_sample_questions,dropdown_samples,samples)
|
593 |
+
|
594 |
+
|
595 |
+
# ---- Guidelines Tab ----
|
596 |
+
with gr.Tab("Guidelines", elem_classes="max-height other-tabs"):
|
597 |
+
gr.Markdown("""
|
598 |
+
#### Welcome to EUDR Q&A, your AI-powered assistant for exploring and understanding EU Deforestation Regulation compliance. This tool leverages advanced language models to help you get clear and structured answers based on EUDR data and reports. To get you started, here are a few tips on how to use the tool:
|
599 |
+
## 💬 Crafting Effective Prompts
|
600 |
+
Clear, specific questions will give you the best results. Here are some examples:
|
601 |
+
| ❌ Less Effective | ✅ More Effective |
|
602 |
+
|------------------|-------------------|
|
603 |
+
| "What is deforestation?" | "What are the main deforestation hotspots identified in Ecuador's latest assessment?" |
|
604 |
+
| "Tell me about compliance" | "What specific EUDR compliance requirements apply to coffee imports from Guatemala?" |
|
605 |
+
| "Show me data" | "What is the deforestation rate trend in the selected region between 2020-2023?" |
|
606 |
+
## ⭐ Best Practices
|
607 |
+
- **Be Clear and Specific**: Frame your questions clearly and focus on what you want to learn.
|
608 |
+
- **One Topic at a Time**: Break complex queries into simpler, focused questions.
|
609 |
+
- **Provide Context**: Mentioning specific regions, commodities, or time periods helps narrow the focus.
|
610 |
+
- **Follow Up**: Ask follow-up questions to explore a topic more deeply.
|
611 |
+
|
612 |
+
## 🔍 Utilizing Data Sources
|
613 |
+
- **GeoJSON Upload**: Upload your GeoJSON files containing geographic data for specific analysis of deforestation patterns in your area of interest.
|
614 |
+
- **Country Reports**: Select from Ecuador or Guatemala to access country-specific EUDR compliance reports and analysis.
|
615 |
+
## 📚 Useful Resources
|
616 |
+
|
617 |
+
- <ins>[**EU Deforestation Regulation Official Guide**](https://environment.ec.europa.eu/topics/forests/deforestation_en)</ins>
|
618 |
+
- <ins>[**EUDR Implementation Timeline**](https://ec.europa.eu/environment/forests/deforestation.htm)</ins>
|
619 |
+
- <ins>[**Commodity-Specific Guidelines**](https://ec.europa.eu/info/food-farming-fisheries/key-policies/common-agricultural-policy/market-measures/agri-food-chain_en)</ins>
|
620 |
+
Enjoy using EUDR Q&A and happy analyzing!
|
621 |
+
""")
|
622 |
+
|
623 |
+
|
624 |
+
|
625 |
+
# static tab 'about us'
|
626 |
+
with gr.Tab("About",elem_classes = "max-height other-tabs"):
|
627 |
+
with gr.Row():
|
628 |
+
with gr.Column(scale=1):
|
629 |
+
gr.Markdown("""The <ins>[**EU Deforestation Regulation (EUDR)**](https://environment.ec.europa.eu/topics/forests/deforestation_en)</ins> \
|
630 |
+
represents a landmark piece of legislation aimed at minimizing the EU's contribution to global deforestation \
|
631 |
+
and forest degradation. The regulation requires companies to ensure that specific commodities placed on the EU market \
|
632 |
+
are deforestation-free and legally produced.
|
633 |
+
|
634 |
+
However, understanding and implementing EUDR compliance can be complex, involving detailed geographic data analysis, \
|
635 |
+
supply chain tracking, and regulatory interpretation. The current format of compliance reports and geographic data \
|
636 |
+
can be challenging for stakeholders to navigate effectively, potentially hindering proper implementation \
|
637 |
+
and compliance monitoring.
|
638 |
+
|
639 |
+
In response to this challenge, modern advancements in Artificial Intelligence (AI), \
|
640 |
+
particularly Retrieval Augmented Generation (RAG) technology and geographic data processing, \
|
641 |
+
emerge as promising solutions. By harnessing the capabilities of such AI tools, \
|
642 |
+
there is an opportunity to improve the accessibility and understanding \
|
643 |
+
of EUDR requirements and geographic risk assessments, ensuring that compliance insights are effectively \
|
644 |
+
translated into actionable outcomes for businesses and regulators.
|
645 |
+
|
646 |
+
To address these implementation challenges, the **GIZ** has initiated this prototype project \
|
647 |
+
to enhance how EUDR compliance data and geographic information are processed and understood. \
|
648 |
+
This AI-powered tool aims to offer critical capabilities such as analyzing complex geographic data, \
|
649 |
+
extracting compliance insights, and enabling interactive, user-friendly analysis through a chatbot interface.
|
650 |
+
|
651 |
+
This prototype tool leveraging AI (Artificial Intelligence) aims at offering critical capabilities such as \
|
652 |
+
summarizing complex compliance requirements, extracting geographic insights from GeoJSON data, \
|
653 |
+
and enabling interactive, user-friendly analysis through a chatbot interface. By making EUDR compliance information more accessible, \
|
654 |
+
this aims to increase understanding and proper implementation among stakeholders, \
|
655 |
+
which can lead to better environmental outcomes and regulatory compliance.
|
656 |
+
|
657 |
+
""")
|
658 |
+
|
659 |
+
|
660 |
+
# static tab for disclaimer
|
661 |
+
with gr.Tab("Disclaimer",elem_classes = "max-height other-tabs"):
|
662 |
+
with gr.Row():
|
663 |
+
with gr.Column(scale=1):
|
664 |
+
gr.Markdown("""
|
665 |
+
- This chatbot is intended for specific use of answering questions based on EUDR compliance reports and uploaded GeoJSON data. For any use beyond this scope we have no liability for responses provided by the chatbot.
|
666 |
+
- The functionality and scope of this chatbot is limited to the context contained in EUDR reports and uploaded geographic data files.
|
667 |
+
- We have implemented measures to ensure the technical robustness and security of our AI system, minimizing unexpected behaviour, however we do not guarantee the full reliability, or completeness of any information provided by the chatbot and disclaim any liability or responsibility for actions taken based on its responses.
|
668 |
+
- The chatbot may occasionally provide inaccurate or inappropriate responses, and it is important to exercise judgment and critical thinking when interpreting its output, especially for compliance decisions.
|
669 |
+
- The use of AI within this application is transparent. When interacting with the AI, users are informed that they are engaging with an AI system.
|
670 |
+
- The chatbot responses should not be considered professional legal or compliance advice and are generated based on patterns in the data it has been trained on.
|
671 |
+
- The chatbot's responses do not reflect the official positions or policies of the EU, our organization or its affiliates regarding EUDR implementation.
|
672 |
+
- Any personal or sensitive information shared with the chatbot is at the user's own risk, and we cannot guarantee complete privacy or confidentiality.
|
673 |
+
- Uploaded GeoJSON files are processed locally and temporarily for analysis purposes. We recommend not uploading sensitive or proprietary geographic data.
|
674 |
+
- The chatbot is not deterministic, so there might be changes in answers to the same question when asked by different users or multiple times.
|
675 |
+
- When you use this app, we collect certain information to understand its usage, analyze performance, and continuously improve the tool for future use. This includes:
|
676 |
+
- The questions you ask
|
677 |
+
- The AI-generated answers
|
678 |
+
- Information about uploaded files (file names, not content)
|
679 |
+
- Feedback given towards each response (in form of Thumbs-up and Thumbs-down), if any.
|
680 |
+
- Usage statistics such as session duration, device type and anonymized geo-location information.
|
681 |
+
|
682 |
+
We process this data based on our legitimate interest in continually enhancing the quality, security, and usability of the EUDR Q&A assistant
|
683 |
+
- By using this chatbot, you agree to these terms and acknowledge that you are solely responsible for any reliance on or actions taken based on its responses.
|
684 |
+
- User can read more about the technical information about the tool in [**Readme**](https://huggingface.co/spaces/GIZ/eudr_assistant/blob/main/README.md) of this tool.
|
685 |
+
- **This is just a prototype and being tested and worked upon, so it's not perfect and may sometimes give irrelevant answers**. If you are not satisfied with the answer, please ask a more specific question or report your feedback to help us improve the system.
|
686 |
+
""")
|
687 |
+
|
688 |
+
|
689 |
+
def show_feedback(logs):
|
690 |
+
"""Show feedback buttons and store logs in state"""
|
691 |
+
return gr.update(visible=True), gr.update(visible=False), logs
|
692 |
+
|
693 |
+
def submit_feedback_okay(logs_data):
|
694 |
+
"""Handle 'okay' feedback submission"""
|
695 |
+
return submit_feedback("okay", logs_data)
|
696 |
+
|
697 |
+
def submit_feedback_not_okay(logs_data):
|
698 |
+
"""Handle 'not okay' feedback submission"""
|
699 |
+
return submit_feedback("not_okay", logs_data)
|
700 |
+
|
701 |
+
okay_btn.click(
|
702 |
+
submit_feedback_okay,
|
703 |
+
[feedback_state],
|
704 |
+
[feedback_row, feedback_thanks]
|
705 |
+
)
|
706 |
+
|
707 |
+
not_okay_btn.click(
|
708 |
+
submit_feedback_not_okay,
|
709 |
+
[feedback_state],
|
710 |
+
[feedback_row, feedback_thanks]
|
711 |
+
)
|
712 |
+
|
713 |
+
#-------------------- Session Management + Geolocation -------------------------
|
714 |
+
|
715 |
+
# Add these state components at the top level of the Blocks
|
716 |
+
session_id = gr.State(None)
|
717 |
+
client_ip = gr.State(None)
|
718 |
+
|
719 |
+
@demo.load(api_name="get_client_ip")
|
720 |
+
def get_client_ip_handler(dummy_input="", request: gr.Request = None):
|
721 |
+
"""Handler for getting client IP in Gradio context"""
|
722 |
+
return get_client_ip(request)
|
723 |
+
|
724 |
+
|
725 |
+
#-------------------- Gradio voodoo -------------------------
|
726 |
+
|
727 |
+
# Update the event handlers
|
728 |
+
(textbox
|
729 |
+
.submit(get_client_ip_handler, [textbox], [client_ip], api_name="get_ip_textbox")
|
730 |
+
.then(start_chat, [textbox, chatbot], [textbox, tabs, chatbot], queue=False, api_name="start_chat_textbox")
|
731 |
+
.then(chat,
|
732 |
+
[textbox, chatbot, search_method, dropdown_country, uploaded_file, client_ip, session_id],
|
733 |
+
[chatbot, sources_textbox, feedback_state, session_id],
|
734 |
+
queue=True, concurrency_limit=8, api_name="chat_textbox")
|
735 |
+
.then(show_feedback, [feedback_state], [feedback_row, feedback_thanks, feedback_state], api_name="show_feedback_textbox")
|
736 |
+
.then(finish_chat, None, [textbox], api_name="finish_chat_textbox"))
|
737 |
+
|
738 |
+
(examples_hidden
|
739 |
+
.change(start_chat, [examples_hidden, chatbot], [textbox, tabs, chatbot], queue=False, api_name="start_chat_examples")
|
740 |
+
.then(get_client_ip_handler, [examples_hidden], [client_ip], api_name="get_ip_examples")
|
741 |
+
.then(chat,
|
742 |
+
[examples_hidden, chatbot, search_method, dropdown_country, uploaded_file, client_ip, session_id],
|
743 |
+
[chatbot, sources_textbox, feedback_state, session_id],
|
744 |
+
concurrency_limit=8, api_name="chat_examples")
|
745 |
+
.then(show_feedback, [feedback_state], [feedback_row, feedback_thanks, feedback_state], api_name="show_feedback_examples")
|
746 |
+
.then(finish_chat, None, [textbox], api_name="finish_chat_examples"))
|
747 |
+
|
748 |
+
demo.queue()
|
749 |
+
|
750 |
demo.launch()
|