Spaces:
Build error
Build error
| import pandas as pd | |
| splits = {'train': 'data/train-00000-of-00001.parquet', 'test': 'data/test-00000-of-00001.parquet'} | |
| df = pd.read_parquet("hf://datasets/pierre-pessarossi/climate-question-answers/" + splits["train"]) | |
| df = df.sample(500) | |
| context_data = [] | |
| for i in range(len(df)): | |
| context = "" | |
| for j in range(2): | |
| column_name = df.columns[j] | |
| value = df.iloc[i][j] | |
| # Handle NoneType values | |
| if value is None: | |
| value = "" | |
| context += f"{column_name}: {value} " | |
| context_data.append(context) | |
| import os | |
| # Get the secret key from the environment | |
| groq_key = os.environ.get('groq_api_keys') | |
| ## LLM used for RAG | |
| from langchain_groq import ChatGroq | |
| llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_key) | |
| ## Embedding model! | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1") | |
| # create vector store! | |
| from langchain_chroma import Chroma | |
| vectorstore = Chroma( | |
| collection_name="climate_education", | |
| embedding_function=embed_model, | |
| persist_directory="./", | |
| ) | |
| # add data to vector nstore | |
| vectorstore.add_texts(context_data) | |
| retriever = vectorstore.as_retriever() | |
| from langchain_core.prompts import PromptTemplate | |
| template = ("""You are a simplified climate education expert. | |
| Use the provided context to answer the question. | |
| If you don't know the answer, say so. Explain your answer in detail. | |
| Do not discuss the context in your response; just provide the answer directly. | |
| Context: {context} | |
| Question: {question} | |
| Answer:""") | |
| rag_prompt = PromptTemplate.from_template(template) | |
| from langchain_core.output_parsers import StrOutputParser | |
| from langchain_core.runnables import RunnablePassthrough | |
| rag_chain = ( | |
| {"context": retriever, "question": RunnablePassthrough()} | |
| | rag_prompt | |
| | llm | |
| | StrOutputParser() | |
| ) | |
| import gradio as gr | |
| def rag_memory_stream(message, history): | |
| partial_text = "" | |
| for new_text in rag_chain.stream(message): | |
| partial_text += new_text | |
| yield partial_text | |
| examples = ['What is climate change', 'What is carbon emission', 'What are the indicators of extreme climates', 'What happens during the completion stage of an oil well?'] | |
| title = "Climate Education Buddy :)" | |
| description = "Real-time AI App with Groq API and LangChain to Provide Simplified Climate-related Answers..." | |
| # demo = gr.Interface( | |
| # title=title, | |
| # fn=rag_memory_stream, | |
| # #inputs="text", | |
| # inputs=gr.Textbox( | |
| # label="Enter Your Question", | |
| # placeholder="E.g., What is gas emission?"), | |
| # #outputs="text", | |
| # outputs=gr.Textbox( | |
| # label="AI Response", | |
| # placeholder="E.g., Gas emissions refer to the release of gases into the atmosphere that contribute to the greenhouse effect and global warming"), | |
| # examples=examples, | |
| # theme="citrus", | |
| # allow_flagging="never", ) | |
| demo = gr.ChatInterface(fn=rag_memory_stream, | |
| type="messages", | |
| title=title, | |
| description=description, | |
| fill_height=True, | |
| examples=examples, | |
| theme="glass", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |