Files changed (1) hide show
  1. app.py +30 -51
app.py CHANGED
@@ -2,36 +2,22 @@ import streamlit as st
2
  import google.generativeai as genai
3
  import os
4
 
5
- # Configure Gemini API with your key from environment variables
6
  api_key = os.getenv("GEMINI_API_KEY")
7
 
8
  if not api_key:
9
- st.error("API key is missing! Please set GEMINI_API_KEY in your Hugging Face Spaces secrets.")
10
  else:
11
  genai.configure(api_key=api_key)
12
 
13
- # Get available models
14
- try:
15
- available_models = [model.name for model in genai.list_models()]
16
- if "gemini-pro" not in available_models:
17
- st.warning(f"'gemini-pro' not found. Using 'gemini-pro-latest' instead.")
18
- model_name = "gemini-pro-latest"
19
- else:
20
- model_name = "gemini-pro"
21
- except Exception as e:
22
- st.error(f"Error fetching available models: {e}")
23
- model_name = "gemini-pro-latest"
24
-
25
  def gemini_generate(prompt):
26
- """Generate content using the Gemini model with error handling."""
27
- try:
28
- response = genai.GenerativeModel(model_name).generate_content(prompt)
29
- return response.text
30
- except Exception as e:
31
- return f"Error generating response: {e}"
32
 
33
  class GeminiAgent:
34
- """A simple agent wrapper that uses the Gemini model for generating responses."""
35
  def __init__(self, role, goal, backstory, verbose=True):
36
  self.role = role
37
  self.goal = goal
@@ -39,7 +25,7 @@ else:
39
  self.verbose = verbose
40
 
41
  def generate(self, prompt):
42
- """Generate a response using Gemini."""
43
  full_prompt = (
44
  f"Role: {self.role}\n"
45
  f"Goal: {self.goal}\n"
@@ -51,17 +37,18 @@ else:
51
  return gemini_generate(full_prompt)
52
 
53
  def create_agents(language="English"):
 
54
  researcher = GeminiAgent(
55
- role="Multilingual Research Analyst",
56
- goal="Analyze challenges and potential solutions for underserved educational institutions",
57
- backstory="Expert in educational data analysis with extensive field experience in underserved regions.",
58
  verbose=True
59
  )
60
 
61
  educator = GeminiAgent(
62
- role="Bilingual Science Educator",
63
- goal=f"Explain the analyzed challenges and solutions in simple terms for {language} speakers",
64
- backstory=f"Expert communicator specializing in translating complex educational research into actionable insights in {language}.",
65
  verbose=True
66
  )
67
 
@@ -72,30 +59,27 @@ else:
72
  RELEVANT_KEYWORDS = {"school", "college", "university", "education", "students", "infrastructure", "learning"}
73
 
74
  def is_relevant_query(user_input):
75
- """Check if the user input is relevant based on predefined keywords."""
76
  return any(keyword in user_input.lower() for keyword in RELEVANT_KEYWORDS)
77
 
78
  def get_chatbot_response(user_input):
79
- """Process the user's query only if it is relevant."""
80
-
81
  if not is_relevant_query(user_input):
82
- return "I'm here to discuss challenges and solutions for schools, colleges, and universities in underserved regions. Please ask a relevant question."
83
 
84
  researcher_prompt = (
85
- "Analyze the following query to identify potential challenges and actionable solutions "
86
- "for schools, colleges, or universities in underserved regions. Provide detailed insights, "
87
- "including possible names of institutions as examples where applicable.\n\n"
88
  f"User Query: {user_input}"
89
  )
90
 
91
  try:
92
  research_response = researcher_agent.generate(researcher_prompt)
93
  except Exception as e:
94
- return f"Error in research agent: {str(e)}"
95
 
96
  educator_prompt = (
97
- "Using the following detailed analysis, explain the challenges and suggested solutions in simple terms. "
98
- "Be sure to mention specific institutions if they are provided.\n\n"
99
  f"Analysis: {research_response}"
100
  )
101
 
@@ -105,24 +89,19 @@ else:
105
  return f"Error in educator agent: {str(e)}"
106
 
107
  combined_response = (
108
- "**Detailed Analysis:**\n"
109
  f"{research_response}\n\n"
110
- "**Simplified Explanation:**\n"
111
  f"{educator_response}"
112
  )
113
  return combined_response
114
 
115
- st.title("EduConnect Underserved Institutions Chatbot")
116
- st.write(
117
- "Ask any question related to schools, colleges, or universities in underserved regions. "
118
- "The chatbot will analyze the challenges, propose actionable solutions, and provide examples of institutions when relevant."
119
- )
120
 
121
  user_input = st.text_input("Enter your question:")
122
-
123
  if st.button("Submit"):
124
- if user_input.strip():
125
- answer = get_chatbot_response(user_input)
126
- st.write(answer)
127
- else:
128
- st.warning("Please enter a question.")
 
2
  import google.generativeai as genai
3
  import os
4
 
5
+ # Configure Gemini API
6
  api_key = os.getenv("GEMINI_API_KEY")
7
 
8
  if not api_key:
9
+ st.error("API key is missing! Please set GEMINI_API_KEY in your environment variables.")
10
  else:
11
  genai.configure(api_key=api_key)
12
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def gemini_generate(prompt):
14
+ """Generate content using the Gemini model."""
15
+ model = genai.GenerativeModel("gemini-pro-latest") # Ensure correct model name
16
+ response = model.generate_content(prompt)
17
+ return response.text
 
 
18
 
19
  class GeminiAgent:
20
+ """AI agent that generates responses using the Gemini model."""
21
  def __init__(self, role, goal, backstory, verbose=True):
22
  self.role = role
23
  self.goal = goal
 
25
  self.verbose = verbose
26
 
27
  def generate(self, prompt):
28
+ """Generate a response using Gemini AI."""
29
  full_prompt = (
30
  f"Role: {self.role}\n"
31
  f"Goal: {self.goal}\n"
 
37
  return gemini_generate(full_prompt)
38
 
39
  def create_agents(language="English"):
40
+ """Create AI agents with specific roles."""
41
  researcher = GeminiAgent(
42
+ role="Educational Researcher",
43
+ goal="Analyze challenges and provide solutions for underserved schools, colleges, and universities.",
44
+ backstory="Expert in education infrastructure and policy for underserved regions.",
45
  verbose=True
46
  )
47
 
48
  educator = GeminiAgent(
49
+ role="Education Communicator",
50
+ goal=f"Explain challenges and solutions in simple terms for {language} speakers.",
51
+ backstory=f"Skilled at translating research into easy-to-understand insights for {language} learners.",
52
  verbose=True
53
  )
54
 
 
59
  RELEVANT_KEYWORDS = {"school", "college", "university", "education", "students", "infrastructure", "learning"}
60
 
61
  def is_relevant_query(user_input):
62
+ """Check if the query is related to education in underserved regions."""
63
  return any(keyword in user_input.lower() for keyword in RELEVANT_KEYWORDS)
64
 
65
  def get_chatbot_response(user_input):
66
+ """Process the query using AI agents."""
 
67
  if not is_relevant_query(user_input):
68
+ return "I'm here to discuss education challenges in underserved regions. Please ask a relevant question."
69
 
70
  researcher_prompt = (
71
+ "Analyze the following query to identify challenges and provide actionable solutions for "
72
+ "schools, colleges, or universities in underserved regions. Use examples where possible.\n\n"
 
73
  f"User Query: {user_input}"
74
  )
75
 
76
  try:
77
  research_response = researcher_agent.generate(researcher_prompt)
78
  except Exception as e:
79
+ return f"Error in researcher agent: {str(e)}"
80
 
81
  educator_prompt = (
82
+ "Explain the research findings in a simple and easy-to-understand manner.\n\n"
 
83
  f"Analysis: {research_response}"
84
  )
85
 
 
89
  return f"Error in educator agent: {str(e)}"
90
 
91
  combined_response = (
92
+ "**πŸ” Research Findings:**\n"
93
  f"{research_response}\n\n"
94
+ "**πŸ“– Simplified Explanation:**\n"
95
  f"{educator_response}"
96
  )
97
  return combined_response
98
 
99
+ # Streamlit UI
100
+ st.title("πŸŽ“ EduConnect Chatbot")
101
+ st.write("Ask about schools, colleges, and universities in underserved regions.")
 
 
102
 
103
  user_input = st.text_input("Enter your question:")
104
+
105
  if st.button("Submit"):
106
+ response = get_chatbot_response(user_input)
107
+ st.write(response)