|
import gradio as gr |
|
import os |
|
import logging |
|
from crewai import Agent, Task, Crew |
|
from langchain_openai import ChatOpenAI |
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") |
|
|
|
|
|
api_key = os.environ.get("OPENAI_API_KEY") |
|
|
|
if not api_key: |
|
logging.error("β ERROR: OpenAI API Key is missing! Add it in Hugging Face Secrets.") |
|
exit(1) |
|
|
|
logging.info("β
OpenAI API Key loaded successfully.") |
|
|
|
|
|
try: |
|
llm = ChatOpenAI(model_name="gpt-4o-mini", temperature=0.7, openai_api_key=api_key) |
|
logging.info("β
ChatOpenAI initialized successfully.") |
|
except Exception as e: |
|
logging.error(f"β ERROR: Failed to initialize ChatOpenAI: {e}") |
|
exit(1) |
|
|
|
|
|
try: |
|
chatbot_agent = Agent( |
|
role="AI Support Assistant", |
|
goal="Provide accurate, helpful, and friendly answers to customer FAQs.", |
|
backstory="You are a highly trained AI customer support assistant with deep knowledge of FAQs.", |
|
llm=llm |
|
) |
|
logging.info("β
AI Agent initialized successfully.") |
|
except Exception as e: |
|
logging.error(f"β ERROR: Failed to initialize AI agent: {e}") |
|
exit(1) |
|
|
|
|
|
faq_list = { |
|
"business hours": "π
We are open from 9 AM to 6 PM, Monday to Friday.", |
|
"track order": "π¦ You can track your order using the tracking link sent to your email.", |
|
"refund policy": "π° We offer a 30-day money-back guarantee. Contact support for details.", |
|
"support contact": "π You can contact our support team at support@example.com or call +1 234 567 890." |
|
} |
|
|
|
|
|
try: |
|
crew = Crew(agents=[chatbot_agent], tasks=[]) |
|
logging.info("β
CrewAI initialized successfully.") |
|
except Exception as e: |
|
logging.error(f"β ERROR: Failed to initialize CrewAI: {e}") |
|
exit(1) |
|
|
|
|
|
def chatbot_response(question): |
|
try: |
|
logging.info(f"π Received question: {question}") |
|
|
|
|
|
for key in faq_list: |
|
if key in question.lower(): |
|
logging.info("β
Matched FAQ response.") |
|
return faq_list[key] |
|
|
|
|
|
task = Task( |
|
description=f"Answer this customer query in a detailed, professional, and helpful manner: '{question}'", |
|
agent=chatbot_agent, |
|
expected_output="A well-structured and relevant response." |
|
) |
|
|
|
|
|
crew.tasks = [task] |
|
response = crew.kickoff() |
|
|
|
if not response: |
|
raise ValueError("β ERROR: CrewAI did not return a response.") |
|
|
|
logging.info(f"β
AI Crew Response: {response}") |
|
return response |
|
|
|
except Exception as e: |
|
logging.error(f"β ERROR in chatbot response: {e}") |
|
return "β οΈ Sorry, something went wrong. Please try again." |
|
|
|
|
|
interface = gr.Interface( |
|
fn=chatbot_response, |
|
inputs=gr.Textbox(label="Ask me anything:", placeholder="Type your question here..."), |
|
outputs=gr.Textbox(label="AI Response"), |
|
title="π€ AI Customer Support Chatbot", |
|
description="Ask me anything, and I'll provide a useful response!", |
|
theme="default" |
|
) |
|
|
|
if __name__ == "__main__": |
|
logging.info("π Launching Gradio on Hugging Face Spaces...") |
|
interface.launch(server_name="0.0.0.0", server_port=7860, share=True) |
|
|
|
|