Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install Required Libraries (if not already installed)
|
| 2 |
+
# !pip install groq sentence-transformers faiss-cpu gradio pandas numpy langchain langchain-community langchain-groq python-dotenv
|
| 3 |
+
|
| 4 |
+
# Import Necessary Libraries
|
| 5 |
+
import os
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
import faiss
|
| 9 |
+
import numpy as np
|
| 10 |
+
import gradio as gr
|
| 11 |
+
from groq import Groq
|
| 12 |
+
from langchain.chains import RetrievalQA
|
| 13 |
+
from langchain.prompts import PromptTemplate
|
| 14 |
+
from langchain.document_loaders import DataFrameLoader
|
| 15 |
+
from langchain.vectorstores import FAISS
|
| 16 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 17 |
+
from langchain_groq import ChatGroq # Updated Import
|
| 18 |
+
|
| 19 |
+
# Step 1: Set up API Key for Groq
|
| 20 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 21 |
+
|
| 22 |
+
# Initialize Groq Client using LangChain Wrapper
|
| 23 |
+
llm = ChatGroq(
|
| 24 |
+
groq_api_key=os.environ.get("GROQ_API_KEY"),
|
| 25 |
+
model="llama3-8b-8192"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Step 2: Load Dataset
|
| 29 |
+
df = pd.read_csv('environmental_impact_assessment.csv')
|
| 30 |
+
|
| 31 |
+
# Step 3: Prepare Text Data for RAG
|
| 32 |
+
# Create a 'text' column combining relevant columns
|
| 33 |
+
df['text'] = (
|
| 34 |
+
"Project Type: " + df['Project Type'].astype(str) + "; " +
|
| 35 |
+
"Land Use: " + df['Land Use (sq km)'].astype(str) + "; " +
|
| 36 |
+
"Emissions: " + df['Emissions (tons/year)'].astype(str) + "; " +
|
| 37 |
+
"Water Requirement: " + df['Water Requirement (liters/day)'].astype(str) + "; " +
|
| 38 |
+
"Mitigation Measures: " + df['Mitigation Measures'].astype(str) + "; " +
|
| 39 |
+
"Legal Compliance: " + df['Legal Compliance'].astype(str)
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Step 4: Create Vector Store for Retrieval
|
| 43 |
+
loader = DataFrameLoader(df, page_content_column="text")
|
| 44 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2") # Explicit model specified
|
| 45 |
+
vectorstore = FAISS.from_documents(loader.load(), embeddings)
|
| 46 |
+
|
| 47 |
+
# Step 5: Build RAG QA Chain
|
| 48 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 49 |
+
llm=llm,
|
| 50 |
+
chain_type="stuff",
|
| 51 |
+
retriever=vectorstore.as_retriever()
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Step 6: Define Gradio Interface
|
| 55 |
+
def generate_report(project_type, land_use, emissions, water_requirement):
|
| 56 |
+
"""
|
| 57 |
+
Generate Environmental Impact Assessment Report using Groq API and RAG.
|
| 58 |
+
"""
|
| 59 |
+
query = (
|
| 60 |
+
f"Generate an environmental impact assessment report for a project with the following details:\n"
|
| 61 |
+
f"Project Type: {project_type}, Land Use: {land_use} sq km, Emissions: {emissions} tons/year, "
|
| 62 |
+
f"Water Requirement: {water_requirement} liters/day."
|
| 63 |
+
)
|
| 64 |
+
try:
|
| 65 |
+
response = qa_chain.run(query)
|
| 66 |
+
return response
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"An error occurred: {e}"
|
| 69 |
+
|
| 70 |
+
# Step 7: Build Gradio Interface
|
| 71 |
+
iface = gr.Interface(
|
| 72 |
+
fn=generate_report,
|
| 73 |
+
inputs=[
|
| 74 |
+
gr.Textbox(label="Project Type"),
|
| 75 |
+
gr.Number(label="Land Use (sq km)"),
|
| 76 |
+
gr.Number(label="Emissions (tons/year)"),
|
| 77 |
+
gr.Number(label="Water Requirement (liters/day)")
|
| 78 |
+
],
|
| 79 |
+
outputs=gr.Textbox(label="Generated Report"),
|
| 80 |
+
title="Environmental Impact Assessment Report Generator",
|
| 81 |
+
description="Enter project details to generate an environmental impact assessment report using RAG and Groq's API."
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Step 8: Launch the Gradio App
|
| 85 |
+
iface.launch()
|