Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from langchain.prompts import PromptTemplate | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| import os, spaces | |
| import pandas as pd | |
| os.environ['GOOGLE_API_KEY'] = os.getenv('geminiapi') | |
| # Function for LLM response | |
| def llm_response(columns, df_text, user_prompt): | |
| # define llm | |
| llm = ChatGoogleGenerativeAI(model="gemini-pro") | |
| # define prompt template | |
| ptemplate = ''' | |
| You are an Expert text analyser to find the insights.we have csv file with following columns: {columns} and For the csv content: {df_text}, | |
| find the insights based on given user question: {user_prompt} | |
| ''' | |
| prompt = PromptTemplate(template=ptemplate,input_variables=['columns','df_text','user_prompt']) | |
| final_prompt = prompt.format(df_text=df_text, user_prompt=user_prompt, columns=columns) | |
| # invoke llm to get result | |
| result = llm.invoke(final_prompt) | |
| # print result on screen | |
| st.subheader("Result:") | |
| st.write(result.content) | |
| def get_user_input(): | |
| user_prompt = st.text_input("Enter User question based on dataframe") | |
| return user_prompt | |
| # define page config | |
| st.set_page_config( | |
| page_title="Blog Generation", | |
| page_icon="🧊", | |
| layout="centered", | |
| initial_sidebar_state="collapsed", | |
| ) | |
| st.header("File Insights🧊") | |
| uploaded_file = st.file_uploader("Upload csv file") | |
| if uploaded_file is not None: | |
| df = pd.read_csv(uploaded_file, encoding = "ISO-8859-1") | |
| st.write(df) | |
| user_prompt = get_user_input() | |
| # Convert the DataFrame to a text-based context for the model | |
| df_text = df.to_string(index=False) # Converts DataFrame to a string for context | |
| # Chat with Google Gemini AI using the DataFrame as context | |
| llm_response(','.join(df.columns), df_text, user_prompt) | |