laxminarasimha6 commited on
Commit
28d6d8a
1 Parent(s): 0ee8abb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sqlite3
3
+ import streamlit as st
4
+ import google.generativeai as genai
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables from .env
8
+ load_dotenv()
9
+
10
+ # Set up the Google API key for Gemini
11
+ api_key = os.getenv("GOOGLE_API_KEY")
12
+ if api_key is None:
13
+ st.error("GOOGLE_API_KEY not found in environment variables. Please check your .env file.")
14
+ else:
15
+ # Configure Google Generative AI API
16
+ genai.configure(api_key=api_key)
17
+
18
+
19
+ # Function to fetch all courses from the SQLite database
20
+ def fetch_all_courses():
21
+ conn = sqlite3.connect('courses.db')
22
+ cur = conn.cursor()
23
+ cur.execute("SELECT title, description, price FROM courses")
24
+ rows = cur.fetchall()
25
+ conn.close()
26
+ return rows
27
+
28
+
29
+ # Function to generate a response using Google Generative AI based on user prompt and available courses
30
+ def generate_response(prompt, courses):
31
+ try:
32
+ # Prepare a detailed context prompt for the LLM
33
+ course_details = "\n".join(
34
+ [f"Title: {course[0]}, Description: {course[1]}, Price: {course[2]}" for course in courses])
35
+
36
+ genai_prompt = f"""
37
+ You are an expert assistant tasked with finding relevant courses based on user queries.
38
+ Below are details of available courses:
39
+
40
+ {course_details}
41
+
42
+ Based on this information, respond to the user's query in the most relevant way:
43
+ {prompt}
44
+ """
45
+
46
+ # Generate a response using Google Generative AI
47
+ model = genai.GenerativeModel('gemini-pro')
48
+ response = model.generate_content([genai_prompt, prompt])
49
+ return response.text.strip() # Return the natural language response
50
+ except Exception as e:
51
+ st.error(f"Error generating a response: {e}")
52
+ return None
53
+
54
+
55
+ # Streamlit interface
56
+ st.set_page_config(page_title="Smart Search for Courses")
57
+ st.header("Find Relevant Courses on Analytics Vidhya")
58
+
59
+ # User prompt input
60
+ user_query = st.text_input("Enter your search query (e.g., 'Show me all free courses on machine learning'):")
61
+
62
+ submit = st.button("Search")
63
+
64
+ # Fetch all courses from the database
65
+ courses = fetch_all_courses()
66
+
67
+ # If user submits the query
68
+ if submit and user_query:
69
+ # Generate a response from Google Generative AI
70
+ response = generate_response(user_query, courses)
71
+
72
+ if response:
73
+ st.subheader("Search Results:")
74
+ st.write(response)
75
+ else:
76
+ st.write("Could not generate a response. Please try again.")