Spaces:
Build error
Build error
| import gradio as gr | |
| import os | |
| import openai | |
| from openai import OpenAI | |
| import dotenv | |
| import time | |
| import re | |
| dotenv.load_dotenv() | |
| threads = [] | |
| # OpenAI API Key | |
| client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | |
| ASSISTANT_ID = os.environ.get("ASSISTANT_ID") | |
| # Get the assistant ID and field ids(reuses if already created) | |
| history = [ | |
| gr.ChatMessage(role="assistant", content="Hello! I can help you understand how school admissions work and explore how policies affect fair access to education. You can ask me about FSM data, admission policies, application processes, appeals—and more. Just tell me your school’s name, or pick a topic below to get started."), | |
| ] | |
| def submit_message(thread, user_message): | |
| print(user_message) | |
| userFileIds = [] | |
| client.beta.threads.messages.create( | |
| thread_id=thread.id, role="user", content=user_message, | |
| ) | |
| return client.beta.threads.runs.create( | |
| thread_id=thread.id, | |
| assistant_id=ASSISTANT_ID, | |
| ) | |
| def create_thread_and_run(user_input): | |
| thread = client.beta.threads.create() | |
| run = submit_message( thread, user_input) | |
| print(run.usage) | |
| return {'thread':thread, 'run':run} | |
| # def checkForExampleChange(history): | |
| # last_message = history[-1].get("content", "").lower() | |
| # if 'admissions' in last_message or 'admission' in last_message: | |
| # print("Example changed") | |
| # examples = [ | |
| # "Tell me about Catchment area admissions", | |
| # "Tell me about Sibling admissions", | |
| # "Tell me about Faith admissions", | |
| # "Tell me about Open admissions", | |
| # "Tell me about Random admissions", | |
| # "Tell me about Priority admissions", | |
| # "Tell me about Banding admissions", | |
| # "Show me research about school admissions", | |
| # ] | |
| # cb.examples= examples | |
| # else: | |
| # print("Example not changed") | |
| def get_response(thread): | |
| history = [] | |
| messages = client.beta.threads.messages.list(thread_id=thread.id, order="desc") | |
| for m in messages: | |
| message_text = m.content[0].text.value | |
| regex_pattern = r"【.*?】" | |
| cleaned_string = re.sub(regex_pattern, '', message_text) | |
| print(f"{m.role}: {m.content[0].text.value}") | |
| obj = {"role" :m.role, "content": cleaned_string} | |
| history.append(obj) | |
| # checkForExampleChange(history) | |
| return history | |
| # Waiting in a loop | |
| def wait_on_run(run, thread): | |
| while run.status == "queued" or run.status == "in_progress": | |
| run = client.beta.threads.runs.retrieve( | |
| thread_id=thread.id, | |
| run_id=run.id, | |
| ) | |
| time.sleep(0.5) | |
| print(run.status) | |
| return run | |
| def process(user_input, history): | |
| toProcess = create_thread_and_run( | |
| user_input | |
| ) | |
| run = wait_on_run(toProcess.get("run"), toProcess.get("thread")) | |
| thread = toProcess.get("thread") | |
| print(run) | |
| if run.status != "completed": | |
| raise Exception(f"Run failed with status: {run.status}") | |
| else: | |
| return get_response(thread)[0].get("content") | |
| css = """ | |
| .placeholder { | |
| display: flex; | |
| justify-content: center; | |
| align-items: center; | |
| padding: 20px; | |
| text-align: center; /* Center text horizontally */ | |
| height:100% | |
| } | |
| .placeholder > p{ | |
| margin: 0; | |
| } | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| title = gr.Markdown( | |
| """ | |
| <h2>Fair Admissions Advisor</h2> | |
| """ | |
| ) | |
| examples = [ | |
| "Compare Free School Meal rates for a school", | |
| "Explain a specific type of admissions policy", | |
| "I’m a parent with a question about admissions", | |
| ] | |
| ci = gr.ChatInterface( | |
| chatbot=gr.Chatbot([], type='messages', placeholder="Hello! I can help you understand how school admissions work and explore how policies affect fair access to education. You can ask me about FSM data, admission policies, application processes, appeals—and more. Just tell me your school’s name, or pick a topic below to get started."), | |
| fn=process, | |
| examples=examples, | |
| type='messages', | |
| ), | |
| demo.launch() | |