Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
import anthropic | |
from dotenv import load_dotenv | |
load_dotenv() | |
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY") | |
# Initialize the Anthropic language model | |
client = anthropic.Anthropic() | |
# Define the chatbot's initial prompt | |
initial_prompt = "Hello! I'm an AI chatbot that can help you find the best school for your child" | |
async def chatbot(child_age, child_grade, child_gender, child_ethnicity, child_talents, desired_location, school_size, school_types, more_options): | |
""" | |
Chatbot function that generates school recommendations directly. | |
""" | |
# Create a prompt with user information | |
user_info = { | |
"child_age": child_age, | |
"child_grade": child_grade, | |
"child_gender": child_gender, | |
"child_ethnicity": child_ethnicity, | |
"child_talents": child_talents, | |
"desired_location": desired_location, | |
"school_size": school_size, | |
"school_types": ", ".join(school_types), | |
"more_options": ", ".join(more_options), | |
} | |
user_info_str = "\n".join([f"{k}: {v}" for k, v in user_info.items()]) | |
prompt = f""" | |
You are a helpful and knowledgeable AI assistant that specializes in providing school recommendations for children. Based on the following user information, provide at least 8 tailored school recommendations: | |
{user_info_str} | |
Follow these guidelines: | |
1. Provide at least 8 school recommendations that best match the user's criteria. Include relevant details such as school name, location, type, and size. | |
2. Along with the recommendations, provide additional information about each school, including school rating, parent testimonials, and pastoral care. | |
3. Always suggest at the bottom of the recommendations that you give, a one-to-one approach or personalized guidance, suggest that they send an email to hello@kiroti-konsult.com. | |
4. Always maintain a friendly, respectful, and professional tone when interacting with users. | |
Remember to focus on providing accurate, relevant, and up-to-date school recommendations that cater to the unique needs and preferences of each user. | |
""" | |
# Use the Anthropic API to generate school recommendations | |
response = client.messages.create( | |
model="claude-3-opus-20240229", | |
max_tokens=2000, | |
temperature=0.0, | |
messages=[{"role": "user", "content": prompt}] | |
) | |
print("Response object:", response) | |
print("Type of response object:", type(response)) | |
# Extract recommendations from the response | |
recommendations = response.content[0].text | |
return recommendations | |
# Create the Gradio interface | |
chatbot_interface = gr.Interface( | |
fn=chatbot, | |
inputs=[ | |
gr.Textbox(lines=1, label="Child's Age"), | |
gr.Textbox(lines=1, label="Child's Grade/Year Group"), | |
gr.Textbox(lines=1, label="Child's Gender"), | |
gr.Textbox(lines=1, label="Child's Nationality"), | |
gr.Textbox(lines=5, label="Child's Talents/Interests"), | |
gr.Textbox(lines=1, label="Desired Location"), | |
gr.Dropdown(["50 - 200", "201 - 400", "400+"], label="School Size"), | |
gr.CheckboxGroup( | |
["Independent/Private", "Faith Schools", "Academies", "Grammar", "Comprehensive", "Free Schools", "State Schools", "Special Needs Schools", | |
"High School", "Elementary School", "Colleges", "6th Form", "Middle School", "Immersion School", "Government Schools"], | |
label="School Types" | |
), | |
gr.CheckboxGroup( | |
["Single Sex Schools", "Co-Educational Schools", "Day Schools", | |
"Boarding Schools", "City Schools", "Countryside Schools"], | |
label="More Options" | |
) | |
], | |
outputs=gr.Textbox(label="Recommendations"), | |
title="School Recommendation Chatbot", | |
description="I'll ask you some questions about your child, and then I'll provide school recommendations." | |
) | |
# Launch the Gradio app | |
chatbot_interface.launch(share=True) |