Spaces:
Build error
Build error
File size: 4,249 Bytes
4dbf463 3b95f93 4dbf463 3b95f93 4dbf463 3b95f93 4dbf463 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 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()
|