Spaces:
Sleeping
Sleeping
Commit
Β·
e88b277
1
Parent(s):
f1abaf7
rollback async solution
Browse files
main.py
CHANGED
@@ -700,7 +700,7 @@ class HealthcareChatbot:
|
|
700 |
else:
|
701 |
return "I apologize, I encountered a processing issue. How can I help you?"
|
702 |
|
703 |
-
|
704 |
"""Make API call to backend with retry logic"""
|
705 |
endpoint_url = data.get('endpoint')
|
706 |
endpoint_method = data.get('method')
|
@@ -713,80 +713,80 @@ class HealthcareChatbot:
|
|
713 |
endpoint_params['patient_id'] = self.user_id
|
714 |
|
715 |
retries = 0
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
|
724 |
-
|
725 |
-
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
-
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
|
734 |
|
735 |
-
|
736 |
-
|
737 |
-
|
738 |
|
739 |
-
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
|
744 |
-
|
745 |
-
|
746 |
-
|
747 |
|
748 |
-
|
749 |
-
async with aiohttp.ClientSession() as session:
|
750 |
-
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
|
764 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
|
785 |
-
|
786 |
-
|
787 |
-
|
788 |
-
|
789 |
-
|
790 |
|
791 |
def handle_api_action(self, user_query, detected_language, sentiment_result, keywords, router_data):
|
792 |
"""Handle API-based actions using router data"""
|
@@ -811,28 +811,29 @@ class HealthcareChatbot:
|
|
811 |
print(f"π Final API call data: {router_data}")
|
812 |
|
813 |
# Make backend API call
|
814 |
-
# try:
|
815 |
-
# # api_response = asyncio.run(self.backend_call(router_data))
|
816 |
-
# loop = asyncio.get_event_loop()
|
817 |
-
# api_response = loop.run_until_complete(self.backend_call(router_data))
|
818 |
-
# loop.close()
|
819 |
-
# except:
|
820 |
-
# print(traceback.format_exc())
|
821 |
try:
|
822 |
-
|
823 |
-
|
824 |
-
|
825 |
-
|
826 |
-
|
827 |
-
finally:
|
828 |
-
new_loop.close()
|
829 |
-
|
830 |
-
import concurrent.futures
|
831 |
-
with concurrent.futures.ThreadPoolExecutor() as executor:
|
832 |
-
future = executor.submit(run_async)
|
833 |
-
api_response = future.result(timeout=30) # 30 second timeout
|
834 |
except:
|
835 |
print(traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
836 |
|
837 |
print("π API response received:", api_response)
|
838 |
|
|
|
700 |
else:
|
701 |
return "I apologize, I encountered a processing issue. How can I help you?"
|
702 |
|
703 |
+
def backend_call(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
704 |
"""Make API call to backend with retry logic"""
|
705 |
endpoint_url = data.get('endpoint')
|
706 |
endpoint_method = data.get('method')
|
|
|
713 |
endpoint_params['patient_id'] = self.user_id
|
714 |
|
715 |
retries = 0
|
716 |
+
response = None
|
717 |
+
while retries < self.max_retries:
|
718 |
+
try:
|
719 |
+
if endpoint_method.upper() == 'GET':
|
720 |
+
response = requests.get(
|
721 |
+
self.BASE_URL + endpoint_url,
|
722 |
+
params=endpoint_params,
|
723 |
+
headers=self.headers,
|
724 |
+
timeout=10
|
725 |
+
)
|
726 |
+
elif endpoint_method.upper() in ['POST', 'PUT', 'DELETE']:
|
727 |
+
response = requests.request(
|
728 |
+
endpoint_method.upper(),
|
729 |
+
self.BASE_URL + endpoint_url,
|
730 |
+
json=endpoint_params,
|
731 |
+
headers=self.headers,
|
732 |
+
timeout=10
|
733 |
+
)
|
734 |
|
735 |
+
response.raise_for_status()
|
736 |
+
print('Backend Response:', response.json())
|
737 |
+
return response.json()
|
738 |
|
739 |
+
except requests.exceptions.RequestException as e:
|
740 |
+
retries += 1
|
741 |
+
if retries >= self.max_retries:
|
742 |
+
return {
|
743 |
+
"error": "Backend API call failed after multiple retries",
|
744 |
+
"details": str(e),
|
745 |
+
"status_code": getattr(e.response, 'status_code', None) if hasattr(e, 'response') else None
|
746 |
+
}
|
747 |
|
748 |
+
time.sleep(self.retry_delay)
|
749 |
+
# async with aiohttp.ClientSession() as session:
|
750 |
+
# while retries < self.max_retries:
|
751 |
+
# try:
|
752 |
+
# if endpoint_method.upper() == 'GET':
|
753 |
+
# response = await session.get(
|
754 |
+
# self.BASE_URL + endpoint_url,
|
755 |
+
# params=endpoint_params,
|
756 |
+
# headers=self.headers,
|
757 |
+
# timeout=aiohttp.ClientTimeout(total=10)
|
758 |
+
# )
|
759 |
+
# return await response.json()
|
760 |
+
|
761 |
+
# elif endpoint_method.upper() in ['POST', 'PUT', 'DELETE']:
|
762 |
+
# response = await session.request(
|
763 |
+
# endpoint_method.upper(),
|
764 |
+
# self.BASE_URL + endpoint_url,
|
765 |
+
# json=endpoint_params,
|
766 |
+
# headers=self.headers,
|
767 |
+
# timeout=aiohttp.ClientTimeout(total=10)
|
768 |
+
# )
|
769 |
+
# return await response.json()
|
770 |
+
|
771 |
+
# except aiohttp.ClientResponseError as e:
|
772 |
+
# retries += 1
|
773 |
+
# if retries >= self.max_retries:
|
774 |
+
# return {
|
775 |
+
# "error": "Backend API call failed after multiple retries",
|
776 |
+
# "details": str(e),
|
777 |
+
# "status_code": e.status
|
778 |
+
# }
|
779 |
+
# await asyncio.sleep(self.retry_delay)
|
780 |
+
|
781 |
+
# except (aiohttp.ClientError, asyncio.TimeoutError) as e:
|
782 |
+
# retries += 1
|
783 |
+
# if retries >= self.max_retries:
|
784 |
+
# return {
|
785 |
+
# "error": "Backend API call failed after multiple retries",
|
786 |
+
# "details": str(e),
|
787 |
+
# "status_code": None
|
788 |
+
# }
|
789 |
+
# await asyncio.sleep(self.retry_delay)
|
790 |
|
791 |
def handle_api_action(self, user_query, detected_language, sentiment_result, keywords, router_data):
|
792 |
"""Handle API-based actions using router data"""
|
|
|
811 |
print(f"π Final API call data: {router_data}")
|
812 |
|
813 |
# Make backend API call
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
814 |
try:
|
815 |
+
api_response = self.backend_call(router_data)
|
816 |
+
# api_response = asyncio.run(self.backend_call(router_data))
|
817 |
+
# loop = asyncio.get_event_loop()
|
818 |
+
# api_response = loop.run_until_complete(self.backend_call(router_data))
|
819 |
+
loop.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
820 |
except:
|
821 |
print(traceback.format_exc())
|
822 |
+
# try:
|
823 |
+
# def run_async():
|
824 |
+
# new_loop = asyncio.new_event_loop()
|
825 |
+
# asyncio.set_event_loop(new_loop)
|
826 |
+
# try:
|
827 |
+
# return new_loop.run_until_complete(self.backend_call(router_data))
|
828 |
+
# finally:
|
829 |
+
# new_loop.close()
|
830 |
+
|
831 |
+
# import concurrent.futures
|
832 |
+
# with concurrent.futures.ThreadPoolExecutor() as executor:
|
833 |
+
# future = executor.submit(run_async)
|
834 |
+
# api_response = future.result(timeout=30) # 30 second timeout
|
835 |
+
# except:
|
836 |
+
# print(traceback.format_exc())
|
837 |
|
838 |
print("π API response received:", api_response)
|
839 |
|