Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import logging
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
from crewai import Agent, Task, Crew
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
|
| 8 |
# Setup logging
|
| 9 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 10 |
|
| 11 |
-
# Load API Key
|
| 12 |
-
|
| 13 |
-
api_key = os.environ.get("OPENAI_API_KEY")
|
| 14 |
-
|
| 15 |
|
| 16 |
if not api_key:
|
| 17 |
-
logging.error("β ERROR: OpenAI API Key is missing! Add it
|
| 18 |
exit(1)
|
| 19 |
|
| 20 |
logging.info("β
OpenAI API Key loaded successfully.")
|
|
@@ -48,7 +45,7 @@ faq_list = {
|
|
| 48 |
"support contact": "π You can contact our support team at support@example.com or call +1 234 567 890."
|
| 49 |
}
|
| 50 |
|
| 51 |
-
# Define CrewAI System
|
| 52 |
try:
|
| 53 |
crew = Crew(agents=[chatbot_agent], tasks=[]) # Initialized with no tasks yet
|
| 54 |
logging.info("β
CrewAI initialized successfully.")
|
|
@@ -56,7 +53,7 @@ except Exception as e:
|
|
| 56 |
logging.error(f"β ERROR: Failed to initialize CrewAI: {e}")
|
| 57 |
exit(1)
|
| 58 |
|
| 59 |
-
# Define
|
| 60 |
def chatbot_response(question):
|
| 61 |
try:
|
| 62 |
logging.info(f"π Received question: {question}")
|
|
@@ -67,14 +64,14 @@ def chatbot_response(question):
|
|
| 67 |
logging.info("β
Matched FAQ response.")
|
| 68 |
return faq_list[key]
|
| 69 |
|
| 70 |
-
#
|
| 71 |
task = Task(
|
| 72 |
description=f"Answer this customer query in a detailed, professional, and helpful manner: '{question}'",
|
| 73 |
agent=chatbot_agent,
|
| 74 |
expected_output="A well-structured and relevant response."
|
| 75 |
)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
crew.tasks = [task]
|
| 79 |
response = crew.kickoff()
|
| 80 |
|
|
@@ -89,24 +86,17 @@ def chatbot_response(question):
|
|
| 89 |
return "β οΈ Sorry, something went wrong. Please try again."
|
| 90 |
|
| 91 |
# Deploy with Gradio UI
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
)
|
| 101 |
-
logging.info("β
Gradio UI initialized successfully.")
|
| 102 |
-
except Exception as e:
|
| 103 |
-
logging.error(f"β ERROR: Failed to initialize Gradio UI: {e}")
|
| 104 |
-
exit(1)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
interface.launch(server_name="127.0.0.1", server_port=None, share=True, debug=True)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
logging.error(f"β ERROR: Failed to launch Gradio app: {e}")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import logging
|
|
|
|
| 4 |
from crewai import Agent, Task, Crew
|
| 5 |
from langchain_openai import ChatOpenAI
|
| 6 |
|
| 7 |
# Setup logging
|
| 8 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 9 |
|
| 10 |
+
# Load API Key from Hugging Face Secrets
|
| 11 |
+
api_key = os.environ.get("OPENAI_API_KEY") # Hugging Face does not support .env files
|
|
|
|
|
|
|
| 12 |
|
| 13 |
if not api_key:
|
| 14 |
+
logging.error("β ERROR: OpenAI API Key is missing! Add it in Hugging Face Secrets.")
|
| 15 |
exit(1)
|
| 16 |
|
| 17 |
logging.info("β
OpenAI API Key loaded successfully.")
|
|
|
|
| 45 |
"support contact": "π You can contact our support team at support@example.com or call +1 234 567 890."
|
| 46 |
}
|
| 47 |
|
| 48 |
+
# Define CrewAI System
|
| 49 |
try:
|
| 50 |
crew = Crew(agents=[chatbot_agent], tasks=[]) # Initialized with no tasks yet
|
| 51 |
logging.info("β
CrewAI initialized successfully.")
|
|
|
|
| 53 |
logging.error(f"β ERROR: Failed to initialize CrewAI: {e}")
|
| 54 |
exit(1)
|
| 55 |
|
| 56 |
+
# Define Chatbot Response Function
|
| 57 |
def chatbot_response(question):
|
| 58 |
try:
|
| 59 |
logging.info(f"π Received question: {question}")
|
|
|
|
| 64 |
logging.info("β
Matched FAQ response.")
|
| 65 |
return faq_list[key]
|
| 66 |
|
| 67 |
+
# Ensure CrewAI has the correct task
|
| 68 |
task = Task(
|
| 69 |
description=f"Answer this customer query in a detailed, professional, and helpful manner: '{question}'",
|
| 70 |
agent=chatbot_agent,
|
| 71 |
expected_output="A well-structured and relevant response."
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# Add the task to Crew
|
| 75 |
crew.tasks = [task]
|
| 76 |
response = crew.kickoff()
|
| 77 |
|
|
|
|
| 86 |
return "β οΈ Sorry, something went wrong. Please try again."
|
| 87 |
|
| 88 |
# Deploy with Gradio UI
|
| 89 |
+
interface = gr.Interface(
|
| 90 |
+
fn=chatbot_response,
|
| 91 |
+
inputs=gr.Textbox(label="Ask me anything:", placeholder="Type your question here..."),
|
| 92 |
+
outputs=gr.Textbox(label="AI Response"),
|
| 93 |
+
title="π€ AI Customer Support Chatbot",
|
| 94 |
+
description="Ask me anything, and I'll provide a useful response!",
|
| 95 |
+
theme="default"
|
| 96 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
+
logging.info("π Launching Gradio on Hugging Face Spaces...")
|
| 100 |
+
interface.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 101 |
|
| 102 |
+
|
|
|