Ali2206 commited on
Commit
5620229
·
verified ·
1 Parent(s): adac5ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -16
app.py CHANGED
@@ -1,15 +1,17 @@
1
  import os
2
  import sys
3
  import json
 
4
  import logging
5
- from fastapi import FastAPI, HTTPException, UploadFile, File
6
  from fastapi.responses import JSONResponse
7
  from fastapi.middleware.cors import CORSMiddleware
8
  from typing import List, Dict, Optional
9
  from datetime import datetime
10
  from pydantic import BaseModel
 
11
 
12
- # Configure logging for Hugging Face Spaces
13
  logging.basicConfig(
14
  level=logging.INFO,
15
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
@@ -32,7 +34,7 @@ except ImportError as e:
32
  app = FastAPI(
33
  title="TxAgent API",
34
  description="API for TxAgent medical document analysis",
35
- version="1.0.0"
36
  )
37
 
38
  # CORS configuration
@@ -44,6 +46,47 @@ app.add_middleware(
44
  allow_headers=["*"],
45
  )
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # Initialize agent at startup
48
  agent = None
49
 
@@ -55,7 +98,7 @@ async def startup_event():
55
  agent = TxAgent(
56
  model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
57
  rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
58
- tool_files_dict={}, # No tool files in this example
59
  enable_finish=True,
60
  enable_rag=False,
61
  force_finish=True,
@@ -69,28 +112,39 @@ async def startup_event():
69
  logger.error(f"Failed to initialize agent: {str(e)}")
70
  raise RuntimeError(f"Failed to initialize agent: {str(e)}")
71
 
72
- class ChatRequest(BaseModel):
73
- message: str
74
- temperature: float = 0.7
75
- max_new_tokens: int = 512
76
- history: Optional[List[Dict]] = None
77
-
78
  @app.post("/chat")
79
  async def chat_endpoint(request: ChatRequest):
80
- """Handle chat conversations"""
81
  try:
82
- logger.info(f"Chat request received: {request.message[:50]}...")
83
- response = agent.chat(
 
 
84
  message=request.message,
85
  history=request.history,
86
  temperature=request.temperature,
87
  max_new_tokens=request.max_new_tokens
88
  )
 
 
 
 
 
 
 
 
 
 
 
 
89
  return JSONResponse({
90
  "status": "success",
91
- "response": response,
92
- "timestamp": datetime.now().isoformat()
 
 
93
  })
 
94
  except Exception as e:
95
  logger.error(f"Chat error: {str(e)}")
96
  raise HTTPException(status_code=500, detail=str(e))
@@ -100,7 +154,8 @@ async def service_status():
100
  """Check service status"""
101
  return {
102
  "status": "running",
103
- "version": "1.0.0",
104
  "model": agent.model_name if agent else "not loaded",
 
105
  "timestamp": datetime.now().isoformat()
106
  }
 
1
  import os
2
  import sys
3
  import json
4
+ import re
5
  import logging
6
+ from fastapi import FastAPI, HTTPException
7
  from fastapi.responses import JSONResponse
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from typing import List, Dict, Optional
10
  from datetime import datetime
11
  from pydantic import BaseModel
12
+ import markdown
13
 
14
+ # Configure logging
15
  logging.basicConfig(
16
  level=logging.INFO,
17
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
 
34
  app = FastAPI(
35
  title="TxAgent API",
36
  description="API for TxAgent medical document analysis",
37
+ version="2.0.0"
38
  )
39
 
40
  # CORS configuration
 
46
  allow_headers=["*"],
47
  )
48
 
49
+ # Request models
50
+ class ChatRequest(BaseModel):
51
+ message: str
52
+ temperature: float = 0.7
53
+ max_new_tokens: int = 512
54
+ history: Optional[List[Dict]] = None
55
+ format: Optional[str] = "clean" # Options: raw, clean, structured, html
56
+
57
+ # Response cleaning functions
58
+ def clean_text_response(text: str) -> str:
59
+ """Basic text cleaning"""
60
+ # Remove excessive newlines and whitespace
61
+ text = re.sub(r'\n\s*\n', '\n\n', text)
62
+ # Remove redundant spaces
63
+ text = re.sub(r'[ ]+', ' ', text)
64
+ # Clean markdown formatting
65
+ text = text.replace("**", "").replace("__", "")
66
+ return text.strip()
67
+
68
+ def structure_medical_response(text: str) -> Dict:
69
+ """Structure medical content into categories"""
70
+ result = {"overview": "", "symptoms": [], "types": {}, "notes": ""}
71
+
72
+ # Extract overview
73
+ overview_end = text.find("Type 1 Diabetes:")
74
+ result["overview"] = clean_text_response(text[:overview_end])
75
+
76
+ # Extract by type
77
+ type_sections = re.split(r'(Type \d Diabetes:)', text[overview_end:])
78
+ current_type = None
79
+
80
+ for section in type_sections:
81
+ if section.startswith("Type "):
82
+ current_type = section.replace(":", "").strip().lower()
83
+ result["types"][current_type] = []
84
+ elif current_type:
85
+ points = [p.strip() for p in section.split('\n') if p.strip()]
86
+ result["types"][current_type] = points
87
+
88
+ return result
89
+
90
  # Initialize agent at startup
91
  agent = None
92
 
 
98
  agent = TxAgent(
99
  model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
100
  rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
101
+ tool_files_dict={},
102
  enable_finish=True,
103
  enable_rag=False,
104
  force_finish=True,
 
112
  logger.error(f"Failed to initialize agent: {str(e)}")
113
  raise RuntimeError(f"Failed to initialize agent: {str(e)}")
114
 
 
 
 
 
 
 
115
  @app.post("/chat")
116
  async def chat_endpoint(request: ChatRequest):
117
+ """Handle chat conversations with formatting options"""
118
  try:
119
+ logger.info(f"Chat request received (format: {request.format})")
120
+
121
+ # Get raw response from agent
122
+ raw_response = agent.chat(
123
  message=request.message,
124
  history=request.history,
125
  temperature=request.temperature,
126
  max_new_tokens=request.max_new_tokens
127
  )
128
+
129
+ # Format response based on request
130
+ formatted_response = {
131
+ "raw": raw_response,
132
+ "clean": clean_text_response(raw_response),
133
+ "structured": structure_medical_response(raw_response),
134
+ "html": markdown.markdown(raw_response)
135
+ }
136
+
137
+ # Select the requested format
138
+ response_content = formatted_response.get(request.format, formatted_response["clean"])
139
+
140
  return JSONResponse({
141
  "status": "success",
142
+ "format": request.format,
143
+ "response": response_content,
144
+ "timestamp": datetime.now().isoformat(),
145
+ "available_formats": list(formatted_response.keys())
146
  })
147
+
148
  except Exception as e:
149
  logger.error(f"Chat error: {str(e)}")
150
  raise HTTPException(status_code=500, detail=str(e))
 
154
  """Check service status"""
155
  return {
156
  "status": "running",
157
+ "version": "2.0.0",
158
  "model": agent.model_name if agent else "not loaded",
159
+ "formats_available": ["raw", "clean", "structured", "html"],
160
  "timestamp": datetime.now().isoformat()
161
  }