Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -93,17 +93,70 @@ def classify_query_with_gemini(query: str):
|
|
| 93 |
def call_external_api(endpoint_url, category, query, files=None):
|
| 94 |
"""External API ko safely call karta hai aur response handle karta hai."""
|
| 95 |
try:
|
| 96 |
-
#
|
| 97 |
if category == "medicine_info":
|
|
|
|
| 98 |
if files:
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
else:
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
else:
|
|
|
|
| 103 |
if files:
|
| 104 |
-
response = requests.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
else:
|
| 106 |
-
response = requests.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# Status code check karein
|
| 109 |
response.raise_for_status()
|
|
@@ -136,6 +189,65 @@ def start_session():
|
|
| 136 |
print(f"Session started: {session_id}")
|
| 137 |
return jsonify({"session_id": session_id}), 200
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
@app.route('/process_query', methods=['POST'])
|
| 140 |
def process_query():
|
| 141 |
data = request.get_json()
|
|
@@ -217,8 +329,13 @@ def process_with_image():
|
|
| 217 |
|
| 218 |
print(f"Session {session_id}: Image received. Preparing to forward to '{category}' API.")
|
| 219 |
|
| 220 |
-
# File payload prepare karein
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
# External API call with error handling
|
| 224 |
api_result = call_external_api(endpoint_url, category, query, files=files_payload)
|
|
@@ -243,6 +360,3 @@ def process_with_image():
|
|
| 243 |
},
|
| 244 |
"category": category
|
| 245 |
})
|
| 246 |
-
|
| 247 |
-
if __name__ == '__main__':
|
| 248 |
-
app.run(debug=True, port=5000)
|
|
|
|
| 93 |
def call_external_api(endpoint_url, category, query, files=None):
|
| 94 |
"""External API ko safely call karta hai aur response handle karta hai."""
|
| 95 |
try:
|
| 96 |
+
# Category-specific request handling based on your actual APIs
|
| 97 |
if category == "medicine_info":
|
| 98 |
+
# Medicine API expects JSON format with "query" parameter
|
| 99 |
if files:
|
| 100 |
+
# For file upload, use multipart form data
|
| 101 |
+
response = requests.post(
|
| 102 |
+
endpoint_url,
|
| 103 |
+
files=files,
|
| 104 |
+
data={"query": query}, # Medicine API uses "query" not "main_query"
|
| 105 |
+
timeout=30
|
| 106 |
+
)
|
| 107 |
else:
|
| 108 |
+
# For text-only queries, use JSON
|
| 109 |
+
response = requests.post(
|
| 110 |
+
endpoint_url,
|
| 111 |
+
json={"query": query}, # Medicine API uses "query" not "main_query"
|
| 112 |
+
headers={'Content-Type': 'application/json'},
|
| 113 |
+
timeout=30
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
elif category == "disease_query":
|
| 117 |
+
# Disease query API - try multiple formats since we're not sure
|
| 118 |
+
if files:
|
| 119 |
+
response = requests.post(
|
| 120 |
+
endpoint_url,
|
| 121 |
+
files=files,
|
| 122 |
+
data={"main_query": query},
|
| 123 |
+
timeout=30
|
| 124 |
+
)
|
| 125 |
+
else:
|
| 126 |
+
# Try JSON first, fallback to form data if needed
|
| 127 |
+
try:
|
| 128 |
+
response = requests.post(
|
| 129 |
+
endpoint_url,
|
| 130 |
+
json={"main_query": query},
|
| 131 |
+
headers={'Content-Type': 'application/json'},
|
| 132 |
+
timeout=30
|
| 133 |
+
)
|
| 134 |
+
except requests.exceptions.HTTPError as json_error:
|
| 135 |
+
if json_error.response.status_code == 415:
|
| 136 |
+
# Try form data format
|
| 137 |
+
response = requests.post(
|
| 138 |
+
endpoint_url,
|
| 139 |
+
data={"main_query": query},
|
| 140 |
+
timeout=30
|
| 141 |
+
)
|
| 142 |
+
else:
|
| 143 |
+
raise
|
| 144 |
+
|
| 145 |
else:
|
| 146 |
+
# For other categories (skin_disease, report_reading)
|
| 147 |
if files:
|
| 148 |
+
response = requests.post(
|
| 149 |
+
endpoint_url,
|
| 150 |
+
files=files,
|
| 151 |
+
data={"main_query": query},
|
| 152 |
+
timeout=30
|
| 153 |
+
)
|
| 154 |
else:
|
| 155 |
+
response = requests.post(
|
| 156 |
+
endpoint_url,
|
| 157 |
+
data={"main_query": query},
|
| 158 |
+
timeout=30
|
| 159 |
+
)
|
| 160 |
|
| 161 |
# Status code check karein
|
| 162 |
response.raise_for_status()
|
|
|
|
| 189 |
print(f"Session started: {session_id}")
|
| 190 |
return jsonify({"session_id": session_id}), 200
|
| 191 |
|
| 192 |
+
# Temporary debugging endpoint
|
| 193 |
+
@app.route('/test_api', methods=['POST'])
|
| 194 |
+
def test_api():
|
| 195 |
+
"""Debugging endpoint to test different API formats"""
|
| 196 |
+
data = request.get_json()
|
| 197 |
+
query = data.get('query', 'test query')
|
| 198 |
+
endpoint_url = data.get('endpoint', 'http://localhost:5001/ask')
|
| 199 |
+
|
| 200 |
+
results = {}
|
| 201 |
+
|
| 202 |
+
# Test 1: JSON format
|
| 203 |
+
try:
|
| 204 |
+
response = requests.post(
|
| 205 |
+
endpoint_url,
|
| 206 |
+
json={"main_query": query},
|
| 207 |
+
headers={'Content-Type': 'application/json'},
|
| 208 |
+
timeout=10
|
| 209 |
+
)
|
| 210 |
+
results['json_format'] = {
|
| 211 |
+
'status_code': response.status_code,
|
| 212 |
+
'content_type': response.headers.get('content-type'),
|
| 213 |
+
'response': response.text[:200] if response.text else 'Empty'
|
| 214 |
+
}
|
| 215 |
+
except Exception as e:
|
| 216 |
+
results['json_format'] = {'error': str(e)}
|
| 217 |
+
|
| 218 |
+
# Test 2: Form data format
|
| 219 |
+
try:
|
| 220 |
+
response = requests.post(
|
| 221 |
+
endpoint_url,
|
| 222 |
+
data={"main_query": query},
|
| 223 |
+
timeout=10
|
| 224 |
+
)
|
| 225 |
+
results['form_format'] = {
|
| 226 |
+
'status_code': response.status_code,
|
| 227 |
+
'content_type': response.headers.get('content-type'),
|
| 228 |
+
'response': response.text[:200] if response.text else 'Empty'
|
| 229 |
+
}
|
| 230 |
+
except Exception as e:
|
| 231 |
+
results['form_format'] = {'error': str(e)}
|
| 232 |
+
|
| 233 |
+
# Test 3: Alternative parameter name
|
| 234 |
+
try:
|
| 235 |
+
response = requests.post(
|
| 236 |
+
endpoint_url,
|
| 237 |
+
json={"query": query},
|
| 238 |
+
headers={'Content-Type': 'application/json'},
|
| 239 |
+
timeout=10
|
| 240 |
+
)
|
| 241 |
+
results['alt_param_json'] = {
|
| 242 |
+
'status_code': response.status_code,
|
| 243 |
+
'content_type': response.headers.get('content-type'),
|
| 244 |
+
'response': response.text[:200] if response.text else 'Empty'
|
| 245 |
+
}
|
| 246 |
+
except Exception as e:
|
| 247 |
+
results['alt_param_json'] = {'error': str(e)}
|
| 248 |
+
|
| 249 |
+
return jsonify(results)
|
| 250 |
+
|
| 251 |
@app.route('/process_query', methods=['POST'])
|
| 252 |
def process_query():
|
| 253 |
data = request.get_json()
|
|
|
|
| 329 |
|
| 330 |
print(f"Session {session_id}: Image received. Preparing to forward to '{category}' API.")
|
| 331 |
|
| 332 |
+
# File payload prepare karein - different for different categories
|
| 333 |
+
if category == "medicine_info":
|
| 334 |
+
# Medicine API expects 'file' as the key
|
| 335 |
+
files_payload = {'file': (file.filename, file.stream, file.mimetype)}
|
| 336 |
+
else:
|
| 337 |
+
# Other APIs might expect 'photo' or 'image'
|
| 338 |
+
files_payload = {'file': (file.filename, file.stream, file.mimetype)}
|
| 339 |
|
| 340 |
# External API call with error handling
|
| 341 |
api_result = call_external_api(endpoint_url, category, query, files=files_payload)
|
|
|
|
| 360 |
},
|
| 361 |
"category": category
|
| 362 |
})
|
|
|
|
|
|
|
|
|