Spaces:
Build error
Build error
Commit
Β·
96bf1ae
1
Parent(s):
d42c48b
requirements updated
Browse files- requirements.txt +0 -0
- utils/chatbot.py +7 -8
- utils/database_helper.py +92 -0
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
utils/chatbot.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
-
import openai
|
| 2 |
from openai import OpenAI
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
client = OpenAI(
|
| 7 |
-
|
| 8 |
-
|
| 9 |
class SpecializedDoctor:
|
| 10 |
def __init__(self, specialty):
|
| 11 |
self.specialty = specialty
|
|
@@ -32,6 +31,7 @@ class SpecializedDoctor:
|
|
| 32 |
)
|
| 33 |
return response.choices[0].message.content.strip()
|
| 34 |
|
|
|
|
| 35 |
def chat_response(message, history):
|
| 36 |
# Assuming you have only one doctor instance:
|
| 37 |
doctor = SpecializedDoctor("Cardiology") # Change here if you have more
|
|
@@ -43,12 +43,11 @@ def chat_response(message, history):
|
|
| 43 |
history = [history]
|
| 44 |
history.append({"role": "user", "content": message})
|
| 45 |
history.append({"role": "assistant", "content": doctor_response})
|
| 46 |
-
return "", history
|
|
|
|
| 47 |
|
| 48 |
# Create the Gradio interface
|
| 49 |
-
iface = gr.ChatInterface(
|
| 50 |
-
chat_response, title="Cardiologist Assistant", theme="dark"
|
| 51 |
-
)
|
| 52 |
|
| 53 |
# Launch the interface
|
| 54 |
iface.launch()
|
|
|
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) # Set your API key securely
|
| 6 |
+
|
| 7 |
+
|
| 8 |
class SpecializedDoctor:
|
| 9 |
def __init__(self, specialty):
|
| 10 |
self.specialty = specialty
|
|
|
|
| 31 |
)
|
| 32 |
return response.choices[0].message.content.strip()
|
| 33 |
|
| 34 |
+
|
| 35 |
def chat_response(message, history):
|
| 36 |
# Assuming you have only one doctor instance:
|
| 37 |
doctor = SpecializedDoctor("Cardiology") # Change here if you have more
|
|
|
|
| 43 |
history = [history]
|
| 44 |
history.append({"role": "user", "content": message})
|
| 45 |
history.append({"role": "assistant", "content": doctor_response})
|
| 46 |
+
return [{"role": "assistant", "content": doctor_response}], history
|
| 47 |
+
|
| 48 |
|
| 49 |
# Create the Gradio interface
|
| 50 |
+
iface = gr.ChatInterface(chat_response, title="Cardiologist Assistant")
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Launch the interface
|
| 53 |
iface.launch()
|
utils/database_helper.py
CHANGED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import pymongo
|
| 4 |
+
from pymongo.errors import PyMongoError
|
| 5 |
+
import os
|
| 6 |
+
import dotenv
|
| 7 |
+
from pymongo.server_api import ServerApi
|
| 8 |
+
|
| 9 |
+
class DatabaseIO:
|
| 10 |
+
def __init__(self, db_name=None, collection_name=None):
|
| 11 |
+
dotenv.load_dotenv()
|
| 12 |
+
|
| 13 |
+
mongo_username = os.environ['MONGO_USERNAME']
|
| 14 |
+
mongo_password = os.environ['MONGO_PASSWORD']
|
| 15 |
+
client_url = os.environ['MONGO_CLIENT_URL_DEV']
|
| 16 |
+
uri = f"mongodb+srv://{mongo_username}:{mongo_password}@{client_url}/?retryWrites=true&w=majority"
|
| 17 |
+
|
| 18 |
+
if not db_name:
|
| 19 |
+
db_name = os.environ['MONGO_DATABASE_NAME']
|
| 20 |
+
if not collection_name:
|
| 21 |
+
collection_name = os.environ['MONGO_COLLECTION']
|
| 22 |
+
|
| 23 |
+
self.client = pymongo.MongoClient(uri, server_api=ServerApi('1'))
|
| 24 |
+
self.db = self.client[db_name]
|
| 25 |
+
self.collection = self.db[collection_name]
|
| 26 |
+
|
| 27 |
+
def insert_document(self, article, collection=None, unique_on='_id', upsert=False):
|
| 28 |
+
|
| 29 |
+
if not collection:
|
| 30 |
+
collection = self.collection
|
| 31 |
+
|
| 32 |
+
article['date_modified'] = datetime.now().utcnow()
|
| 33 |
+
existing_document = collection.find_one({unique_on: article[unique_on]})
|
| 34 |
+
if existing_document:
|
| 35 |
+
# there is something like with the reddit id already
|
| 36 |
+
|
| 37 |
+
if upsert:
|
| 38 |
+
collection.update_one({unique_on: article[unique_on]}, {"$set": article})
|
| 39 |
+
else:
|
| 40 |
+
article['date_created'] = datetime.now().utcnow()
|
| 41 |
+
collection.insert_one(article)
|
| 42 |
+
|
| 43 |
+
def __enter__(self):
|
| 44 |
+
return self
|
| 45 |
+
|
| 46 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
| 47 |
+
try:
|
| 48 |
+
self.client.close()
|
| 49 |
+
except PyMongoError as e:
|
| 50 |
+
print(f"An error occurred while closing the database connection: {e}")
|
| 51 |
+
raise
|
| 52 |
+
|
| 53 |
+
def __del__(self):
|
| 54 |
+
try:
|
| 55 |
+
self.client.close()
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(e)
|
| 58 |
+
|
| 59 |
+
def read_documents(self, query=None, sort_by=None, sort_order=None):
|
| 60 |
+
if query is None:
|
| 61 |
+
query = {}
|
| 62 |
+
try:
|
| 63 |
+
if sort_by:
|
| 64 |
+
if not sort_order or sort_order not in [1, -1]:
|
| 65 |
+
sort_order= 1
|
| 66 |
+
for article in self.collection.find(query).sort(sort_by, sort_order):
|
| 67 |
+
yield article
|
| 68 |
+
else:
|
| 69 |
+
for article in self.collection.find(query):
|
| 70 |
+
yield article
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(e)
|
| 73 |
+
|
| 74 |
+
def count_documents(self, query=None):
|
| 75 |
+
if query is None:
|
| 76 |
+
query = {}
|
| 77 |
+
try:
|
| 78 |
+
return self.collection.count_documents(query)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(e)
|
| 81 |
+
|
| 82 |
+
def update_documents(self, query, update, upsert=True):
|
| 83 |
+
try:
|
| 84 |
+
self.collection.update_one(query, update, upsert=upsert)
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(e)
|
| 87 |
+
|
| 88 |
+
def delete_document(self, query):
|
| 89 |
+
try:
|
| 90 |
+
self.collection.delete_one(query)
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(e)
|