pasupuletkarthiksai commited on
Commit
61fbe43
·
verified ·
1 Parent(s): 693d37c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -32
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from src.auth.auth import handle_login
3
  from src.auth.db import initialize_db
@@ -8,41 +9,57 @@ from src.interface import Interface
8
  initialize_db()
9
  load_dotenv()
10
 
11
- bot = None # Initially, no bot is created until the user logs in or registers
12
-
13
 
14
  def start_bot(userid, password, api_key_input):
15
- global bot
16
  login_status = handle_login(userid, password, api_key_input)
17
-
18
- if "successful" in login_status: # Check for successful login
19
- bot = Interface() # Initialize after login success
20
  return (
21
- login_status,
22
  gr.update(visible=False), # Hide login/registration section
23
- gr.update(visible=True) # Show chat section
24
  )
25
  else:
26
  return (
27
  login_status,
28
- gr.update(visible=True), # Keep login/registration section visible
29
- gr.update(visible=False) # Keep chat section hidden
30
  )
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def answer(message, history):
34
- answer_md, tables_display, images_display, retrieved_display = bot.get_answer(message)
35
-
36
- # Combine all parts into a single response string for chat
37
- combined_response = f"{answer_md}\n\n{tables_display}"
38
 
39
- # Add images as markdown
40
- if images_display:
41
- combined_response += "\n\n" + "\n\n".join(images_display)
42
-
43
- return combined_response
44
 
 
 
45
 
 
 
 
46
 
47
  with gr.Blocks(fill_height=True, fill_width=True) as app:
48
 
@@ -57,8 +74,9 @@ with gr.Blocks(fill_height=True, fill_width=True) as app:
57
 
58
  with gr.TabItem("Register"):
59
  gr.Markdown("## 🔐 Enter Your Groq Cloud API Key")
60
- gr.Markdown("You can create an API key at [Groq Cloud Console]"
61
- "(https://console.groq.com/keys)")
 
62
  userid_register = gr.Textbox(label="UserID")
63
  password_register = gr.Textbox(label="Password", type="password")
64
  api_key_register = gr.Textbox(
@@ -67,31 +85,38 @@ with gr.Blocks(fill_height=True, fill_width=True) as app:
67
  placeholder="sk-... (required)"
68
  )
69
  register_btn = gr.Button("Register")
70
- register_output = gr.Textbox(label="Registration Status",
71
- interactive=False)
72
 
73
  # Chat Section (Initially hidden)
74
  with gr.Column(visible=False) as chat_section:
75
  gr.ChatInterface(
76
- answer,
77
- title="🩺 Medico-Bot",
78
- examples=["briefly explain me about cancer", "types of skin diseases?"],
79
- flagging_options = ['Like', 'Dislike']
80
- )
81
-
82
 
83
- # Function connections
84
  login_btn.click(
85
  start_bot,
86
- inputs=[userid_login, password_login],
87
  outputs=[login_output, login_register_section, chat_section]
 
 
 
 
88
  )
89
 
 
90
  register_btn.click(
91
  start_bot,
92
  inputs=[userid_register, password_register, api_key_register],
93
  outputs=[register_output, login_register_section, chat_section]
 
 
 
 
94
  )
95
 
96
- app.queue()
97
  app.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
 
1
+ import os
2
  import gradio as gr
3
  from src.auth.auth import handle_login
4
  from src.auth.db import initialize_db
 
9
  initialize_db()
10
  load_dotenv()
11
 
12
+ bot = None
13
+ bot_ready = False
14
 
15
  def start_bot(userid, password, api_key_input):
16
+ global bot_ready
17
  login_status = handle_login(userid, password, api_key_input)
18
+
19
+ if "" in login_status:
20
+ bot_ready = False
21
  return (
22
+ "Login successful. Setting up your bot, please wait...",
23
  gr.update(visible=False), # Hide login/registration section
24
+ gr.update(visible=False) # Still hide chat section for now
25
  )
26
  else:
27
  return (
28
  login_status,
29
+ gr.update(visible=True), # Keep login/register open
30
+ gr.update(visible=False) # Hide chat section
31
  )
32
 
33
+ def setup_bot():
34
+ global bot, bot_ready
35
+ try:
36
+ bot = Interface() # Interface will pick up API key from environment
37
+ bot_ready = True
38
+ return (
39
+ gr.update(visible=False), # Hide login/registration
40
+ gr.update(visible=True) # Show chat section
41
+ )
42
+ except Exception as e:
43
+ return (
44
+ gr.update(visible=True),
45
+ gr.update(visible=False)
46
+ )
47
 
48
  def answer(message, history):
49
+ if bot is None or not bot_ready:
50
+ return "Please log in and wait for your bot to be ready."
 
 
51
 
52
+ try:
53
+ answer_md, tables_display, images_display, retrieved_display = bot.get_answer(message)
54
+
55
+ combined_response = f"{answer_md}\n\n{tables_display}"
 
56
 
57
+ if images_display:
58
+ combined_response += "\n\n" + "\n\n".join(images_display)
59
 
60
+ return combined_response
61
+ except Exception as e:
62
+ return f"An error occurred: {e}"
63
 
64
  with gr.Blocks(fill_height=True, fill_width=True) as app:
65
 
 
74
 
75
  with gr.TabItem("Register"):
76
  gr.Markdown("## 🔐 Enter Your Groq Cloud API Key")
77
+ gr.Markdown(
78
+ "You can create an API key at [Groq Cloud Console](https://console.groq.com/keys)"
79
+ )
80
  userid_register = gr.Textbox(label="UserID")
81
  password_register = gr.Textbox(label="Password", type="password")
82
  api_key_register = gr.Textbox(
 
85
  placeholder="sk-... (required)"
86
  )
87
  register_btn = gr.Button("Register")
88
+ register_output = gr.Textbox(label="Registration Status", interactive=False)
 
89
 
90
  # Chat Section (Initially hidden)
91
  with gr.Column(visible=False) as chat_section:
92
  gr.ChatInterface(
93
+ answer,
94
+ title="🩺 Medico-Bot",
95
+ examples=["briefly explain me about cancer", "types of skin diseases?"],
96
+ flagging_options=['Like', 'Dislike']
97
+ )
 
98
 
99
+ # Login button connection
100
  login_btn.click(
101
  start_bot,
102
+ inputs=[userid_login, password_login, api_key_register],
103
  outputs=[login_output, login_register_section, chat_section]
104
+ ).then(
105
+ setup_bot,
106
+ inputs=[],
107
+ outputs=[login_register_section, chat_section]
108
  )
109
 
110
+ # Register button connection
111
  register_btn.click(
112
  start_bot,
113
  inputs=[userid_register, password_register, api_key_register],
114
  outputs=[register_output, login_register_section, chat_section]
115
+ ).then(
116
+ setup_bot,
117
+ inputs=[],
118
+ outputs=[login_register_section, chat_section]
119
  )
120
 
121
+ app.queue(concurrency_count=1, max_size=5)
122
  app.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)