Soham Mhatre commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
| 4 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 5 |
+
from langchain_groq import ChatGroq
|
| 6 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
+
@st.cache_data
|
| 8 |
+
def load_books():
|
| 9 |
+
return pd.read_csv('books.csv')
|
| 10 |
+
|
| 11 |
+
books_df = load_books()
|
| 12 |
+
|
| 13 |
+
st.title("Book Recommendations App")
|
| 14 |
+
|
| 15 |
+
# Sidebar for CSV-based recommendations
|
| 16 |
+
st.sidebar.header("CSV-based Recommendations")
|
| 17 |
+
csv_genre = st.sidebar.selectbox("Select a genre (CSV)", books_df['Genre'].unique())
|
| 18 |
+
|
| 19 |
+
if csv_genre:
|
| 20 |
+
st.sidebar.subheader(f"Books in {csv_genre} genre:")
|
| 21 |
+
genre_books = books_df[books_df['Genre'] == csv_genre]
|
| 22 |
+
for _, row in genre_books.iterrows():
|
| 23 |
+
st.sidebar.write(f"[{row['Name']}]({row['URL']})")
|
| 24 |
+
|
| 25 |
+
# Main content for Groq-based recommendations
|
| 26 |
+
st.header("LLM-based Recommendations")
|
| 27 |
+
|
| 28 |
+
# Groq API settings
|
| 29 |
+
model_name = st.text_input("Choose your favuorite model", ["llama3-8b-8192", "gemma-7b-It", "mixtral-8x7b-32768", "llama3-70b-8192"])
|
| 30 |
+
groq_genre = st.selectbox("Select a genre (Groq)", ["Science Fiction", "Fantasy", "Mystery", "Romance", "Thriller", "Horror", "Historical Fiction", "Non-fiction"])
|
| 31 |
+
|
| 32 |
+
if st.button("Get Recommendations"):
|
| 33 |
+
if api_key and groq_genre:
|
| 34 |
+
with st.spinner("Generating recommendations..."):
|
| 35 |
+
model = ChatGroq(model=model_name, api_key=)
|
| 36 |
+
parser = StrOutputParser()
|
| 37 |
+
|
| 38 |
+
messages = [
|
| 39 |
+
SystemMessage(content="You're a book recommendation expert."),
|
| 40 |
+
HumanMessage(content=f"Generate 100 book recommendations for the {groq_genre} genre. Provide the output as a numbered list of book titles.")
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
response = parser.invoke(model.invoke(messages))
|
| 44 |
+
|
| 45 |
+
st.subheader(f"Book Recommendations for {groq_genre}:")
|
| 46 |
+
st.write(response)
|
| 47 |
+
else:
|
| 48 |
+
st.error("Please enter your Groq API key and select a genre.")
|
| 49 |
+
|
| 50 |
+
st.sidebar.markdown("## Guide")
|
| 51 |
+
st.sidebar.info("This tool uses GroQ inferencing to generate book recommendations. Select a genre from the CSV data on the left, and use the Groq-based recommendations in the main area.")
|
| 52 |
+
st.sidebar.info("By [Your Name]")
|