anasfsd123 commited on
Commit
768c17c
ยท
verified ยท
1 Parent(s): e89a33c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -92
app.py CHANGED
@@ -1,107 +1,46 @@
1
  import streamlit as st
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
24
- self.backstory = backstory
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"
32
- f"Backstory: {self.backstory}\n\n"
33
- f"{prompt}"
34
- )
35
- if self.verbose:
36
- print("Agent Prompt:", full_prompt)
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
-
55
- return researcher, educator
56
-
57
- researcher_agent, educator_agent = create_agents(language="English")
58
-
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
-
86
- try:
87
- educator_response = educator_agent.generate(educator_prompt)
88
  except Exception as e:
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)
 
 
 
 
 
1
  import streamlit as st
 
2
  import os
3
+ from groq import Groq
4
 
5
+ # Get Groq API Key from environment variables
6
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
7
 
8
+ if not GROQ_API_KEY:
9
+ st.error("API key is missing! Please set GROQ_API_KEY in your environment variables.")
10
  else:
11
+ client = Groq(api_key=GROQ_API_KEY)
12
+
13
+ def get_groq_response(user_input):
14
+ """Generate a response using the Groq API with the LLaMA model and system prompt."""
15
+ system_prompt = (
16
+ "Analyze the following query to identify potential challenges and actionable solutions "
17
+ "for schools, colleges, or universities in underserved regions. Provide detailed insights, "
18
+ "including possible names of institutions as examples where applicable.\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  f"User Query: {user_input}"
20
  )
21
 
22
  try:
23
+ chat_completion = client.chat.completions.create(
24
+ messages=[
25
+ {"role": "system", "content": system_prompt},
26
+ {"role": "user", "content": user_input}
27
+ ],
28
+ model="llama3-70b-8192"
29
+ )
30
+ return chat_completion.choices[0].message.content
 
 
 
31
  except Exception as e:
32
+ return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
33
 
34
  # Streamlit UI
35
+ st.title("EduConnect Chatbot (Groq LLaMA-3 70B)")
36
+ st.write("Ask about challenges and solutions for schools, colleges, or universities in underserved regions.")
37
 
38
  user_input = st.text_input("Enter your question:")
39
+
40
  if st.button("Submit"):
41
+ if user_input.strip():
42
+ response = get_groq_response(user_input)
43
+ st.write("### Response:")
44
+ st.write(response)
45
+ else:
46
+ st.warning("Please enter a question.")