Prak2005 commited on
Commit
1e5d9f3
Β·
verified Β·
1 Parent(s): 5a643d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -28
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
- load_dotenv()
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 to your .env file.")
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 (Fixed: Define crew before using it)
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 Chat Task (Fix: Ensure CrewAI is used properly)
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
- # Fix: Ensure CrewAI has the correct task
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
- # Fix: Add the task to the existing Crew instance
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
- try:
93
- interface = gr.Interface(
94
- fn=chatbot_response,
95
- inputs=gr.Textbox(label="Ask me anything:", placeholder="Type your question here..."),
96
- outputs=gr.Textbox(label="AI Response"),
97
- title="πŸ€– AI Customer Support Chatbot",
98
- description="Ask me anything, and I'll provide a useful response!",
99
- theme="default"
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
- try:
108
- logging.info("πŸš€ Launching Gradio on http://127.0.0.1:7860")
109
- interface.launch(server_name="127.0.0.1", server_port=None, share=True, debug=True)
110
 
111
- except Exception as e:
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
+