Spaces:
Running
Running
File size: 4,594 Bytes
542c765 | 1 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 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | """
Complete workflow: Upload PDF → Get Analysis → Chat with Dr. Raahat
"""
import requests
import json
import sys
BASE_URL = "http://localhost:8000"
def upload_and_analyze_pdf(pdf_path):
"""Upload PDF and get analysis."""
print(f"\n📄 Uploading: {pdf_path}")
print("-" * 60)
with open(pdf_path, 'rb') as f:
files = {'file': f}
response = requests.post(f"{BASE_URL}/analyze", files=files)
if response.status_code != 200:
print(f"Error: {response.status_code}")
return None
analysis = response.json()
print(f"✓ Analysis complete!")
print(f" Readable: {analysis['is_readable']}")
print(f" Severity: {analysis['severity_level']}")
print(f" Summary: {analysis['overall_summary_english']}\n")
return analysis
def chat_with_doctor_about_report(analysis):
"""Interactive dialogue about the uploaded report."""
if not analysis:
print("No analysis available to discuss.")
return
# Build patient context from analysis
patient_context = {
"name": "User",
"age": 45,
"gender": "Not specified",
"language": "EN",
"latestReport": analysis,
"mentalWellness": {"stressLevel": 5, "sleepQuality": 6}
}
print("\n" + "="*70)
print("💬 Dr. Raahat - Your Health Advisor")
print("="*70)
print(f"\nReport Summary: {analysis['overall_summary_english']}")
print(f"Severity Level: {analysis['severity_level']}")
print(f"Affected Organs: {', '.join(analysis['affected_organs'])}")
print(f"\nKey Findings:")
for finding in analysis['findings'][:5]: # Show first 5
if finding['status'] in ['LOW', 'HIGH', 'CRITICAL']:
print(f" • {finding['parameter']}: {finding['value']} {finding['unit']} [{finding['status']}]")
print("\nType 'exit' to end. Type 'findings' to see all results.")
print("="*70)
conversation_history = []
# Initial doctor greeting about the report
initial_response = requests.post(
f"{BASE_URL}/chat",
json={
"message": "What should I know about my report?",
"history": [],
"guc": patient_context
}
)
if initial_response.status_code == 200:
initial_message = initial_response.json()['reply']
print(f"\nDr. Raahat: {initial_message}\n")
while True:
user_input = input("You: ").strip()
if user_input.lower() == 'exit':
print("\nDr. Raahat: Take care! Follow the recommendations and schedule a follow-up in 4 weeks.")
break
if user_input.lower() == 'findings':
print("\nAll Findings from Your Report:")
for i, finding in enumerate(analysis['findings'], 1):
status_icon = "⚠️" if finding['status'] in ['LOW', 'HIGH', 'CRITICAL'] else "✓"
print(f" {status_icon} {finding['parameter']}: {finding['value']} {finding['unit']} ({finding['status']})")
print()
continue
if not user_input:
continue
# Add to history
conversation_history.append({"role": "user", "content": user_input})
# Get doctor response
try:
response = requests.post(
f"{BASE_URL}/chat",
json={
"message": user_input,
"history": conversation_history,
"guc": patient_context
}
)
if response.status_code == 200:
doctor_reply = response.json()['reply']
conversation_history.append({"role": "assistant", "content": doctor_reply})
print(f"\nDr. Raahat: {doctor_reply}\n")
else:
print(f"Error: {response.status_code}\n")
except Exception as e:
print(f"Connection error: {e}")
print("Make sure server is running!\n")
def main():
if len(sys.argv) < 2:
print("Usage: python doctor_workflow.py <pdf_path>")
print("\nExample:")
print(" python doctor_workflow.py C:\\path\\to\\report.pdf")
sys.exit(1)
pdf_path = sys.argv[1]
# Step 1: Upload and analyze
analysis = upload_and_analyze_pdf(pdf_path)
# Step 2: Chat about results
if analysis:
chat_with_doctor_about_report(analysis)
else:
print("Could not analyze report.")
if __name__ == "__main__":
main()
|