ChE112_AI_Tools_2025 / code /20241226_AI-System-8-Design-Thinking.py
Shruteek's picture
Upload 20241226_AI-System-8-Design-Thinking.py
8c8ada2 verified
Raw
History Blame Contribute Delete
3.72 kB
import os
import openai
import gradio as gr
course_username = os.environ["COURSE_USERNAME"]
george_password = os.environ["george_PWD"]
patill_password = os.environ["patill_PWD"]
erini_password = os.environ["erini_PWD"]
claire_password = os.environ["claire_PWD"]
shruteek_password = os.environ["shruteek_PWD"]
samuel_password = os.environ["samuel_PWD"]
george_api_key = os.environ["george_KEY"]
patill_api_key = os.environ["patill_KEY"]
erini_api_key = os.environ["erini_KEY"]
claire_api_key = os.environ["claire_KEY"]
shruteek_api_key = os.environ["shruteek_KEY"]
samuel_api_key = os.environ["samuel_KEY"]
password_to_api_key_dict = {
george_password: george_api_key,
patill_password: patill_api_key,
erini_password: erini_api_key,
claire_password: claire_api_key,
shruteek_password: shruteek_api_key,
samuel_password: samuel_api_key
}
class Conversation:
def __init__(self):
self.assistant_id = ""
self.thread_id = ""
def create_thread(self):
new_thread = client.beta.threads.create()
self.thread_id = new_thread.id
def respond_to_query(self, query, history):
query_message = client.beta.threads.messages.create(
thread_id=self.thread_id, role="user", content=query
)
streamed_run = client.beta.threads.runs.create(
thread_id=self.thread_id,
assistant_id=self.assistant_id,
stream=True
)
current_response = "The Design Thinkerer: "
for event in streamed_run:
if event.event == "thread.message.delta":
for new_text in event.data.delta.content:
current_response += new_text.text.value
yield current_response
current_conversation = Conversation()
with gr.Blocks(fill_height=True) as demo:
username_textbox = gr.Textbox(label="Username")
password_textbox = gr.Textbox(label="Password")
login_btn = gr.Button("Login")
assistant_id_textbox = gr.Textbox(label="Assistant ID", visible=False)
submit_btn = gr.Button("Submit", visible=False)
with gr.Column(visible=False) as chat_container:
AI_chatbot = gr.ChatInterface(fn=current_conversation.respond_to_query, type="messages")
def login_to_webapp(username, password):
if (username == course_username) and (password in password_to_api_key_dict.keys()):
global client
student_api_key = password_to_api_key_dict[password]
client = openai.OpenAI(api_key=student_api_key)
return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)]
else:
gr.Info('The Username or Password was incorrect.')
return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)]
elements_changed_by_login = [username_textbox, password_textbox, login_btn, assistant_id_textbox, submit_btn]
def set_assistant_and_show_chatbot(assistant_id):
current_conversation.assistant_id = assistant_id
current_conversation.create_thread()
return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)]
elements_changed_for_assistant = [assistant_id_textbox, submit_btn, chat_container]
login_btn.click(
inputs=[username_textbox, password_textbox],
fn=login_to_webapp,
outputs=elements_changed_by_login
)
submit_btn.click(
inputs=[assistant_id_textbox],
fn=set_assistant_and_show_chatbot,
outputs=elements_changed_for_assistant
)
demo.launch()