Spaces:
Running
Running
| import os | |
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| from ai_chatbot import AIChatbot, chat_with_bot_via_gateway, get_faqs_via_gateway | |
| from database_recommender import CourseRecommender, get_course_recommendations_ui | |
| import warnings | |
| import logging | |
| import requests | |
| # Suppress warnings | |
| warnings.filterwarnings('ignore') | |
| logging.getLogger('tensorflow').setLevel(logging.ERROR) | |
| # Initialize components | |
| try: | |
| chatbot = AIChatbot() | |
| print("β Chatbot initialized successfully") | |
| except Exception as e: | |
| print(f"β οΈ Warning: Could not initialize chatbot: {e}") | |
| chatbot = None | |
| try: | |
| recommender = CourseRecommender() | |
| print("β Recommender initialized successfully") | |
| except Exception as e: | |
| print(f"β οΈ Warning: Could not initialize recommender: {e}") | |
| recommender = None | |
| GATEWAY_BASE_URL = os.environ.get('GATEWAY_BASE_URL', 'https://database-46m3.onrender.com') | |
| def chat_with_bot(message, history): | |
| """Handle chatbot interactions via gateway-aware helper""" | |
| return chat_with_bot_via_gateway(chatbot, message, history, gateway_base_url=GATEWAY_BASE_URL) | |
| def get_course_recommendations(stanine, gwa, strand, hobbies): | |
| return get_course_recommendations_ui(recommender, stanine, gwa, strand, hobbies) | |
| def get_faqs(): | |
| return get_faqs_via_gateway(gateway_base_url=GATEWAY_BASE_URL) | |
| def get_available_courses(): | |
| """Get available courses""" | |
| if recommender and recommender.courses: | |
| course_text = "## π Available Courses\n\n" | |
| for code, name in recommender.courses.items(): | |
| course_text += f"**{code}** - {name}\n" | |
| return course_text | |
| return "No courses available at the moment." | |
| # Create Gradio interface | |
| with gr.Blocks(title="PSAU AI Chatbot & Course Recommender", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown( | |
| """ | |
| # π€ PSAU AI Chatbot & Course Recommender | |
| Welcome to the Pangasinan State University AI-powered admission assistant! | |
| Get instant answers to your questions and receive personalized course recommendations. | |
| """ | |
| ) | |
| with gr.Tabs(): | |
| # Chatbot Tab | |
| with gr.Tab("π€ AI Chatbot"): | |
| gr.Markdown(""" | |
| **Chat with the PSAU AI Assistant!** | |
| I can help you with: | |
| β’ University admission questions | |
| β’ Course information and guidance | |
| β’ General conversation | |
| β’ Academic support | |
| Just type your message below and I'll respond naturally! | |
| """) | |
| chatbot_interface = gr.ChatInterface( | |
| fn=chat_with_bot, | |
| title="PSAU AI Assistant", | |
| description="Chat with me about university admissions, courses, or just say hello!", | |
| examples=[ | |
| "Hello!", | |
| "What are the admission requirements?", | |
| "How are you?", | |
| "What courses are available?", | |
| "Tell me about PSAU", | |
| "What can you help me with?", | |
| "Thank you", | |
| "Goodbye" | |
| ], | |
| cache_examples=True | |
| ) | |
| # Course Recommender Tab | |
| with gr.Tab("π― Course Recommender"): | |
| gr.Markdown(""" | |
| Get personalized course recommendations based on your academic profile and interests! | |
| **Input Guidelines:** | |
| - **Stanine Score**: Enter a number between 1-9 (from your entrance exam) | |
| - **GWA**: Enter your General Weighted Average (75-100) | |
| - **Strand**: Select your senior high school strand | |
| - **Hobbies**: Describe your interests and hobbies in detail | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| stanine_input = gr.Textbox( | |
| label="Stanine Score (1-9)", | |
| placeholder="Enter your stanine score (1-9)", | |
| info="Your stanine score from entrance examination", | |
| value="7" | |
| ) | |
| gwa_input = gr.Textbox( | |
| label="GWA (75-100)", | |
| placeholder="Enter your GWA (75-100)", | |
| info="Your General Weighted Average", | |
| value="85.0" | |
| ) | |
| strand_input = gr.Dropdown( | |
| choices=["STEM", "ABM", "HUMSS"], | |
| value="STEM", | |
| label="High School Strand", | |
| info="Your senior high school strand" | |
| ) | |
| hobbies_input = gr.Textbox( | |
| label="Hobbies & Interests", | |
| placeholder="e.g., programming, gaming, business, teaching, healthcare...", | |
| info="Describe your interests and hobbies" | |
| ) | |
| recommend_btn = gr.Button("Get Recommendations", variant="primary") | |
| with gr.Column(): | |
| recommendations_output = gr.Markdown() | |
| recommend_btn.click( | |
| fn=get_course_recommendations, | |
| inputs=[stanine_input, gwa_input, strand_input, hobbies_input], | |
| outputs=recommendations_output | |
| ) | |
| # Information Tab | |
| with gr.Tab("π Information"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("### FAQ Section") | |
| faq_btn = gr.Button("Show FAQs") | |
| faq_output = gr.Markdown() | |
| faq_btn.click(fn=get_faqs, outputs=faq_output) | |
| with gr.Column(): | |
| gr.Markdown("### Available Courses") | |
| courses_btn = gr.Button("Show Courses") | |
| courses_output = gr.Markdown() | |
| courses_btn.click(fn=get_available_courses, outputs=courses_output) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False, | |
| show_error=True | |
| ) |