Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from datetime import datetime
|
3 |
+
from enum import Enum
|
4 |
+
from pymongo import MongoClient
|
5 |
+
import PIL
|
6 |
+
import google.generativeai as genai
|
7 |
+
import re
|
8 |
+
from langchain_groq import ChatGroq
|
9 |
+
from langchain.agents import AgentExecutor, create_tool_calling_agent
|
10 |
+
from langchain_core.prompts import ChatPromptTemplate
|
11 |
+
from langchain_core.tools import tool
|
12 |
+
|
13 |
+
# MongoDB connection
|
14 |
+
client = MongoClient('mongodb://miniproject:[email protected]:27017,ac-yzyqqis-shard-00-01.iv8gz1x.mongodb.net:27017,ac-yzyqqis-shard-00-02.iv8gz1x.mongodb.net:27017/?ssl=true&replicaSet=atlas-ayivip-shard-0&authSource=admin&retryWrites=true&w=majority')
|
15 |
+
db = client['Final_Year_Project']
|
16 |
+
collection = db['Patients']
|
17 |
+
|
18 |
+
# Langchain Groq API setup
|
19 |
+
groq_api_key = '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'
|
20 |
+
llm_agent = ChatGroq(api_key=groq_api_key, model='llama-3.3-70b-versatile', temperature=0.1)
|
21 |
+
|
22 |
+
# Set up the prompt template
|
23 |
+
prompt = ChatPromptTemplate.from_messages(
|
24 |
+
[
|
25 |
+
(
|
26 |
+
"system",
|
27 |
+
f"You Are an AI Assistant which helps to manage the reminders for Patients. It is currently {datetime.now()}.",
|
28 |
+
),
|
29 |
+
("human", "{input}"),
|
30 |
+
("placeholder", "{agent_scratchpad}"),
|
31 |
+
]
|
32 |
+
)
|
33 |
+
|
34 |
+
# Enum for reminder status
|
35 |
+
class ReminderStatus(Enum):
|
36 |
+
ACTIVE = "Active"
|
37 |
+
COMPLETED = "Completed"
|
38 |
+
CANCELLED = "Cancelled"
|
39 |
+
|
40 |
+
# Tool for saving user data to MongoDB
|
41 |
+
@tool
|
42 |
+
def save_user_data(user_id: str, patient_name: str, dr_name: str, prescription_date: datetime,
|
43 |
+
age: int, sex: str, medicines: list, notification_type: str = "Push",
|
44 |
+
notification_time: int = 30, status: ReminderStatus = ReminderStatus.ACTIVE):
|
45 |
+
reminder = {
|
46 |
+
"user_id": user_id,
|
47 |
+
"patient_name": patient_name,
|
48 |
+
"dr_name": dr_name,
|
49 |
+
"prescription_date": prescription_date.isoformat(),
|
50 |
+
"age": age,
|
51 |
+
"sex": sex,
|
52 |
+
"medicines": medicines,
|
53 |
+
"notification": {
|
54 |
+
"type": notification_type,
|
55 |
+
"time_before": notification_time
|
56 |
+
},
|
57 |
+
"status": status.value,
|
58 |
+
"created_at": datetime.now()
|
59 |
+
}
|
60 |
+
|
61 |
+
result = collection.insert_one(reminder)
|
62 |
+
return result.inserted_id
|
63 |
+
|
64 |
+
tools = [save_user_data]
|
65 |
+
agent = create_tool_calling_agent(llm_agent, tools, prompt)
|
66 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools)
|
67 |
+
|
68 |
+
# API key for Google Generative AI
|
69 |
+
genai.configure(api_key="AIzaSyCltTyFKRtgLQBR9BwX0q9e2aDW9BwwUfo")
|
70 |
+
|
71 |
+
# Function to process the image and generate text using Google AI
|
72 |
+
def extract_and_store_text(file, user_id):
|
73 |
+
try:
|
74 |
+
# Open the image using PIL
|
75 |
+
image = PIL.Image.open(file)
|
76 |
+
|
77 |
+
# Generate text using Google Generative AI
|
78 |
+
mod = genai.GenerativeModel(model_name="gemini-2.0-flash")
|
79 |
+
text = mod.generate_content(contents=["Extract all text from this image, including medication names, dosages, instructions, patient information, and any other relevant medical details if this is a prescription. If the image is not a prescription, return only 'None'.", image])
|
80 |
+
|
81 |
+
pattern = r'[_*]+\s*'
|
82 |
+
data_str = re.sub(pattern, '', text.text)
|
83 |
+
|
84 |
+
if not text.text:
|
85 |
+
return "No relevant text found"
|
86 |
+
|
87 |
+
else:
|
88 |
+
res = agent_executor.invoke({"input": f'Add this Data to Mongo DB for user with user_id {user_id} DATA : {str(data_str)}'})
|
89 |
+
ans = collection.find_one({'user_id': user_id}, {'_id': 0})
|
90 |
+
return str(ans)
|
91 |
+
|
92 |
+
except Exception as e:
|
93 |
+
return str(e)
|
94 |
+
|
95 |
+
# Gradio interface
|
96 |
+
def gradio_interface(file, user_id):
|
97 |
+
return extract_and_store_text(file, user_id)
|
98 |
+
|
99 |
+
# Define Gradio Inputs and Outputs
|
100 |
+
inputs = [
|
101 |
+
gr.File(label="Upload Image"),
|
102 |
+
gr.Textbox(label="User ID", value="101") # Default user ID
|
103 |
+
]
|
104 |
+
outputs = gr.Textbox(label="Response")
|
105 |
+
|
106 |
+
# Create Gradio Interface
|
107 |
+
interface = gr.Interface(
|
108 |
+
fn=gradio_interface,
|
109 |
+
inputs=inputs,
|
110 |
+
outputs=outputs,
|
111 |
+
title="Patient Reminder System",
|
112 |
+
description="Extracts and stores patient prescription details from images."
|
113 |
+
)
|
114 |
+
|
115 |
+
if __name__ == "__main__":
|
116 |
+
interface.launch()
|