rukeshpaudel commited on
Commit
fd9c287
Β·
1 Parent(s): a507faa

State mgmt

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -6,6 +6,13 @@ from openai import OpenAI
6
 
7
  client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
8
 
 
 
 
 
 
 
 
9
  # Initialize the client
10
  # Set your OpenAI API key
11
 
@@ -36,26 +43,21 @@ assistant = client.beta.assistants.create(
36
  #tools=[{"type": "retrieval"}]
37
  )
38
 
39
- thread = client.beta.threads.create()
40
-
41
- def create_thread():
42
- thread = client.beta.threads.create()
43
- return thread.id
44
-
45
-
46
  def main(query, history):
47
- # Step 2: Create a Thread
48
-
 
 
49
 
50
  # Step 3: Add a Message to a Thread
51
  history = (history,)
52
  message = client.beta.threads.messages.create(
53
- thread_id=thread.id, role="user", content=query
54
  )
55
 
56
  # Step 4: Run the Assistant
57
  run = client.beta.threads.runs.create(
58
- thread_id=thread.id,
59
  assistant_id=assistant.id,
60
  instructions="User is a health patient, who is suffering from {disease}. You are supposed to create a medical report based on the symptoms. If you are 100% sure, you can also predict the disease else just report the symptoms in a formal formatted diagnosis report.\
61
  Make sure to include all the vital informations by asking the patients. Ask their name, address and gender before beginning asking for symptoms. Ask one question at a time so that the user doesn't feel overwhelmed. Ask if they have any more symptoms and after completing asking for the detailed symptoms, generate the symptoms in a medical report like format along with the patient's information.",
@@ -67,12 +69,12 @@ def main(query, history):
67
 
68
  # Retrieve the run status
69
  run_status = client.beta.threads.runs.retrieve(
70
- thread_id=thread.id, run_id=run.id
71
  )
72
 
73
  # If run is completed, get messages
74
  if run_status.status == "completed":
75
- messages = client.beta.threads.messages.list(thread_id=thread.id)
76
  response = ""
77
 
78
  data = messages.data
@@ -88,7 +90,7 @@ def main(query, history):
88
  with gr.Blocks() as iface:
89
  with gr.Tab("MeroHealthAI Chatbot"):
90
  # gr.Markdown("MeroHealthAI is an AI assited chatbot that gathers symptoms from the user, documents it and sends it to the nearest most relevant doctor available. Our app also suppors medical report analysis")
91
- gr.State(value=None)
92
  symptom_chatbot = gr.ChatInterface(
93
  main,
94
  clear_btn="Find Relevant Doctors"
 
6
 
7
  client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
8
 
9
+ def create_thread():
10
+ thread = client.beta.threads.create()
11
+ return thread.id
12
+
13
+ # Initialize thread ID for the current session
14
+ thread_id = create_thread()
15
+
16
  # Initialize the client
17
  # Set your OpenAI API key
18
 
 
43
  #tools=[{"type": "retrieval"}]
44
  )
45
 
 
 
 
 
 
 
 
46
  def main(query, history):
47
+ global thread_id # Ensure we're using the global thread_id variable
48
+ # Generate a new thread ID if it's a new session
49
+ if not history:
50
+ thread_id = create_thread()
51
 
52
  # Step 3: Add a Message to a Thread
53
  history = (history,)
54
  message = client.beta.threads.messages.create(
55
+ thread_id=thread_id, role="user", content=query
56
  )
57
 
58
  # Step 4: Run the Assistant
59
  run = client.beta.threads.runs.create(
60
+ thread_id=thread_id,
61
  assistant_id=assistant.id,
62
  instructions="User is a health patient, who is suffering from {disease}. You are supposed to create a medical report based on the symptoms. If you are 100% sure, you can also predict the disease else just report the symptoms in a formal formatted diagnosis report.\
63
  Make sure to include all the vital informations by asking the patients. Ask their name, address and gender before beginning asking for symptoms. Ask one question at a time so that the user doesn't feel overwhelmed. Ask if they have any more symptoms and after completing asking for the detailed symptoms, generate the symptoms in a medical report like format along with the patient's information.",
 
69
 
70
  # Retrieve the run status
71
  run_status = client.beta.threads.runs.retrieve(
72
+ thread_id=thread_id, run_id=run.id
73
  )
74
 
75
  # If run is completed, get messages
76
  if run_status.status == "completed":
77
+ messages = client.beta.threads.messages.list(thread_id=thread_id)
78
  response = ""
79
 
80
  data = messages.data
 
90
  with gr.Blocks() as iface:
91
  with gr.Tab("MeroHealthAI Chatbot"):
92
  # gr.Markdown("MeroHealthAI is an AI assited chatbot that gathers symptoms from the user, documents it and sends it to the nearest most relevant doctor available. Our app also suppors medical report analysis")
93
+
94
  symptom_chatbot = gr.ChatInterface(
95
  main,
96
  clear_btn="Find Relevant Doctors"