File size: 2,939 Bytes
460edf1 e216d49 460edf1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# Import necessary libraries
import os
import pandas as pd
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import gradio as gr
from groq import Groq
# Set up Groq API
api = os.environ.get('GroqApi')
client = Groq(api_key=api)
# # Load environmental dataset (upload to Colab)
# from google.colab import files
# uploaded = files.upload() # Upload 'environmental_impact_assessment_dataset.csv'
# Load the dataset
df = pd.read_csv('environmental_impact_assessment_dataset.csv') # Replace with the uploaded file name
# Combine relevant text columns for embeddings
text_column = df['Project Type'] + ' ' + df['Mitigation Measures'] # Adjust based on your dataset columns
# Use SentenceTransformers to generate text embeddings
embedding_model = SentenceTransformer('all-MiniLM-L6-v2') # Lightweight embedding model
embeddings = embedding_model.encode(text_column.tolist())
# Convert embeddings to numpy array
embeddings_np = np.array(embeddings).astype(np.float32)
# Build FAISS index for document retrieval
index = faiss.IndexFlatL2(embeddings_np.shape[1]) # L2 distance for similarity search
index.add(embeddings_np) # Add the document embeddings to the FAISS index
# Function to retrieve the most relevant document from the dataset
def retrieve_relevant_document(query):
# Generate query embedding
query_embedding = embedding_model.encode([query])
query_embedding_np = np.array(query_embedding).astype(np.float32)
# Perform similarity search in FAISS
_, indices = index.search(query_embedding_np, k=1) # Top 1 match
retrieved_text = text_column.iloc[indices[0][0]] # Retrieve corresponding text
return retrieved_text
# Function to generate an EIA report using Groq's API
def generate_report(user_input):
# Check if input is empty
if not user_input.strip():
return "Please provide project details to generate the Environmental Impact Assessment report."
# Retrieve relevant information using FAISS
relevant_document = retrieve_relevant_document(user_input)
# Use Groq API to generate a report based on the retrieved document
chat_completion = client.chat.completions.create(
messages=[
{"role": "user",
"content": f"Generate an environmental impact assessment report based on the following details:\n\n{relevant_document}\n\nUser Query: {user_input}"}
],
model="llama3-8b-8192", # Groq model
)
# Return the Groq-generated content
return chat_completion.choices[0].message.content
# Gradio interface for user interaction
def gradio_interface(project_details):
return generate_report(project_details)
# Launch Gradio app
iface = gr.Interface(
fn=gradio_interface,
inputs="text", # Input: text box for project details
outputs="text", # Output: text box for the generated report
live=False # Set to False for non-live mode
)
iface.launch()
|