Files changed (3) hide show
  1. demo.py +57 -14
  2. password_generator.py +16 -0
  3. requirements.txt +1 -1
demo.py CHANGED
@@ -2,6 +2,7 @@ from openai import OpenAI, AssistantEventHandler
2
  from dotenv import load_dotenv
3
  import os
4
  import gradio as gr
 
5
 
6
  # Load environment variables from .env file
7
  load_dotenv()
@@ -9,6 +10,7 @@ load_dotenv()
9
  # Load env variables
10
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
  ASSISTANT_ID = os.getenv("ASSISTANT_ID")
 
12
 
13
  client = OpenAI(api_key=OPENAI_API_KEY)
14
 
@@ -16,9 +18,6 @@ def create_thread():
16
  return client.beta.threads.create()
17
 
18
  def predict(user_message, history, thread):
19
- # Append the new user message to the history
20
- # history.append({"role": "user", "content": user_message})
21
-
22
  # Send the user message to the OpenAI API
23
  client.beta.threads.messages.create(
24
  thread_id=thread.id,
@@ -42,15 +41,62 @@ def predict(user_message, history, thread):
42
  history.append({"role": "assistant", "content": ''.join(response)})
43
  yield history, thread
44
 
45
- # Launch the Gradio chat interface
46
- with gr.Blocks(title="Hair Library Shopping Assistant Demo") as demo:
47
- chatbot = gr.Chatbot(type='messages', label="Hair Library Shopping Assistant Demo")
48
- msg = gr.Textbox(placeholder="Type your message here...")
49
- user_content = gr.State("")
50
- thread = gr.State(create_thread)
51
- history = gr.State([])
52
- submit = gr.Button("Submit")
 
 
 
 
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def user_input(user_message, history):
55
  user_content = user_message
56
  return "", history + [{"role": "user", "content": user_message}], user_content
@@ -62,7 +108,4 @@ with gr.Blocks(title="Hair Library Shopping Assistant Demo") as demo:
62
  predict, [user_content, chatbot, thread], [chatbot, thread]
63
  )
64
 
65
- # msg.submit(predict, [msg, chatbot, thread], [chatbot, thread], queue=False)
66
- # clear.click(lambda: None, None, chatbot, queue=False)
67
-
68
  demo.launch(share=False)
 
2
  from dotenv import load_dotenv
3
  import os
4
  import gradio as gr
5
+ import hashlib
6
 
7
  # Load environment variables from .env file
8
  load_dotenv()
 
10
  # Load env variables
11
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
  ASSISTANT_ID = os.getenv("ASSISTANT_ID")
13
+ PASSWORD_HASH = os.getenv("PASSWORD") # Stored password hash in .env
14
 
15
  client = OpenAI(api_key=OPENAI_API_KEY)
16
 
 
18
  return client.beta.threads.create()
19
 
20
  def predict(user_message, history, thread):
 
 
 
21
  # Send the user message to the OpenAI API
22
  client.beta.threads.messages.create(
23
  thread_id=thread.id,
 
41
  history.append({"role": "assistant", "content": ''.join(response)})
42
  yield history, thread
43
 
44
+ def verify_password(password):
45
+ """Verify password against the stored hash"""
46
+ if not PASSWORD_HASH:
47
+ return False, "Password not configured in environment variables."
48
+
49
+ # Hash the input password
50
+ hashed_input = hashlib.sha256(password.encode()).hexdigest()
51
+
52
+ # Compare with stored hash
53
+ if hashed_input == PASSWORD_HASH:
54
+ return True, "Authentication successful"
55
+ else:
56
+ return False, "Incorrect password. Please try again."
57
 
58
+ # Launch the Gradio chat interface
59
+ with gr.Blocks(title="TM Tender Assistant") as demo:
60
+ # Login state
61
+ is_authenticated = gr.State(False)
62
+
63
+ # Login component
64
+ with gr.Group(visible=True) as login_group:
65
+ gr.Markdown("## TM Tender Assistant - Login")
66
+ password_input = gr.Textbox(type="password", label="Password", placeholder="Enter password")
67
+ login_button = gr.Button("Login")
68
+ login_message = gr.Markdown("")
69
+
70
+ # Chat interface component
71
+ with gr.Group(visible=False) as chat_group:
72
+ gr.Markdown("## TM Tender Assistant")
73
+ chatbot = gr.Chatbot(type='messages', label="TM Tender Assistant")
74
+ msg = gr.Textbox(placeholder="Type your message here...")
75
+ user_content = gr.State("")
76
+ thread = gr.State(create_thread)
77
+ history = gr.State([])
78
+ submit = gr.Button("Submit")
79
+
80
+ def login(password):
81
+ success, message = verify_password(password)
82
+ if success:
83
+ return {
84
+ login_group: gr.update(visible=False),
85
+ chat_group: gr.update(visible=True),
86
+ login_message: ""
87
+ }
88
+ else:
89
+ return {
90
+ login_message: message,
91
+ password_input: ""
92
+ }
93
+
94
+ login_button.click(
95
+ login,
96
+ inputs=[password_input],
97
+ outputs=[login_group, chat_group, login_message]
98
+ )
99
+
100
  def user_input(user_message, history):
101
  user_content = user_message
102
  return "", history + [{"role": "user", "content": user_message}], user_content
 
108
  predict, [user_content, chatbot, thread], [chatbot, thread]
109
  )
110
 
 
 
 
111
  demo.launch(share=False)
password_generator.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import hashlib
2
+ import getpass
3
+
4
+ def generate_password_hash():
5
+ """Generate a SHA-256 hash for a password"""
6
+ password = getpass.getpass("Enter the password you want to hash: ")
7
+ if not password:
8
+ print("Password cannot be empty")
9
+ return
10
+
11
+ hashed = hashlib.sha256(password.encode()).hexdigest()
12
+ print("\nAdd the following line to your .env file:")
13
+ print(f"PASSWORD={hashed}")
14
+
15
+ if __name__ == "__main__":
16
+ generate_password_hash()
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
  gradio==5.3.0
2
- openai==1.52.1
3
  python-dotenv==1.0.1
 
1
  gradio==5.3.0
2
+ openai
3
  python-dotenv==1.0.1