Spaces:
Running
Running
File size: 6,183 Bytes
21dfff9 52f4b0f 21dfff9 52f4b0f 21dfff9 52f4b0f 21dfff9 52f4b0f 21dfff9 52f4b0f 21dfff9 52f4b0f 21dfff9 52f4b0f 21dfff9 52f4b0f 21dfff9 52f4b0f |
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import os
import gradio as gr
import tempfile
import json
from agent import MedTranscriptAgent
from dotenv import load_dotenv
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("app.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
load_dotenv()
if not os.getenv("ANTHROPIC_API_KEY"):
logger.error("ANTHROPIC_API_KEY not found in environment variables or .env file")
raise ValueError("ANTHROPIC_API_KEY is required. Please add it to your .env file.")
agent = MedTranscriptAgent(debug=True)
conversation_threads = {}
def process_message(message, conversation_id=None, pdf_file=None):
if not conversation_id:
import uuid
conversation_id = str(uuid.uuid4())
conversation_threads[conversation_id] = True
logger.info(f"Created new conversation with ID: {conversation_id}")
if pdf_file is not None:
temp_dir = tempfile.mkdtemp()
temp_path = os.path.join(temp_dir, "uploaded.pdf")
with open(temp_path, "wb") as f:
f.write(pdf_file)
pdf_id = agent.load_pdf(temp_path)
logger.info(f"Loaded PDF '{pdf_id}' for conversation {conversation_id}")
logger.info(f"Processing message for conversation {conversation_id}: {message}")
response = agent.chat(message, thread_id=conversation_id)
if hasattr(agent, "conversation_threads") and conversation_id in agent.conversation_threads:
thread_msgs = agent.conversation_threads[conversation_id]
logger.info(f"Thread {conversation_id} now has {len(thread_msgs)} messages")
return response, conversation_id
with gr.Blocks(title="Medical Transcript Q&A System") as demo:
gr.Markdown("# Medical Transcript Q&A System")
gr.Markdown("Ask questions about medical procedures, treatments, or general medical information.")
conversation_id = gr.State(None)
with gr.Row():
with gr.Column(scale=3):
chatbot = gr.Chatbot(
height=500,
type="messages",
avatar_images=("👤", "🩺"),
label="Conversation"
)
msg = gr.Textbox(
label="Your Question",
placeholder="Ask a medical question...",
lines=3
)
with gr.Row():
submit_btn = gr.Button("Submit", variant="primary", scale=2)
clear_btn = gr.Button("Clear Conversation", scale=1)
with gr.Column(scale=1):
pdf_input = gr.File(
label="Upload PDF Document (Optional)",
file_types=[".pdf"],
type="binary"
)
debug_info = gr.Textbox(
label="Debug Info",
visible=True,
interactive=False,
lines=5
)
def user(message, history, conv_id, pdf):
"""Add user message to chat history"""
logger.info(f"User message: {message}")
logger.info(f"Current history length: {len(history) if history else 0}")
logger.info(f"Current conversation ID: {conv_id}")
if history is None:
history = []
history.append({"role": "user", "content": message})
return "", history, conv_id, pdf
def bot(history, conv_id, pdf):
"""Process user message and add bot response to chat history"""
if not history or len(history) == 0:
return history, conv_id, pdf, "Error: No message to process"
user_message = history[-1]["content"]
logger.info(f"Processing user message: {user_message[:50]}...")
try:
response, new_conv_id = process_message(user_message, conv_id, pdf)
history.append({"role": "assistant", "content": response})
thread_msg_count = 0
if hasattr(agent, "conversation_threads") and new_conv_id in agent.conversation_threads:
thread_msg_count = len(agent.conversation_threads[new_conv_id])
debug_text = f"Conversation ID: {new_conv_id}\n"
debug_text += f"UI Messages: {len(history)}\n"
debug_text += f"Agent Thread Messages: {thread_msg_count}\n"
debug_text += f"PDF Uploaded: {'Yes' if pdf else 'No'}\n"
return history, new_conv_id, None, debug_text
except Exception as e:
error_msg = str(e)
logger.error(f"Error processing message: {error_msg}")
history.append({"role": "assistant", "content": f"Error: {error_msg}"})
return history, conv_id, None, f"Error occurred: {error_msg}"
def clear_conversation():
"""Clear the current conversation and start a new one"""
logger.info("Clearing conversation history")
import uuid
new_id = str(uuid.uuid4())
logger.info(f"Created new conversation with ID: {new_id}")
return new_id, gr.update(value=None), [], None, f"Started new conversation with ID: {new_id}"
msg.submit(
user,
[msg, chatbot, conversation_id, pdf_input],
[msg, chatbot, conversation_id, pdf_input]
).then(
bot,
[chatbot, conversation_id, pdf_input],
[chatbot, conversation_id, pdf_input, debug_info]
)
submit_btn.click(
user,
[msg, chatbot, conversation_id, pdf_input],
[msg, chatbot, conversation_id, pdf_input]
).then(
bot,
[chatbot, conversation_id, pdf_input],
[chatbot, conversation_id, pdf_input, debug_info]
)
clear_btn.click(
clear_conversation,
None,
[conversation_id, msg, chatbot, pdf_input, debug_info]
)
if __name__ == "__main__":
logger.info("Starting Medical Transcript Q&A System...")
logger.info(f"API Key present: {'Yes' if os.getenv('ANTHROPIC_API_KEY') else 'No'}")
demo.launch() |