File size: 3,051 Bytes
fa8df3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
import os
import gradio as gr
from groq import Groq
import numpy as np
import faiss
from sentence_transformers import SentenceTransformer

# Initialize Groq API Client
GROQ_API_KEY = "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"
client = Groq(api_key=GROQ_API_KEY)

# Load Pretrained Embedding Model
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")

# Load Sample Environmental Dataset (Replace with your own)
environmental_data = [
    {"text": "Deforestation leads to loss of biodiversity.", "category": "Biodiversity"},
    {"text": "Construction projects can increase carbon emissions.", "category": "Air Quality"},
    {"text": "Water usage must be monitored to prevent scarcity.", "category": "Water Resources"},
    # Add more entries as needed
]

# Generate embeddings for the dataset
def create_dataset_index(data):
    texts = [entry["text"] for entry in data]
    embeddings = embedding_model.encode(texts)
    faiss_index = faiss.IndexFlatL2(embeddings.shape[1])
    faiss_index.add(np.array(embeddings))
    return faiss_index, data

index, indexed_data = create_dataset_index(environmental_data)

# Function to retrieve relevant data
def retrieve_relevant_data(query, top_k=3):
    query_embedding = embedding_model.encode([query])
    distances, indices = index.search(np.array(query_embedding), top_k)
    relevant_texts = [indexed_data[i]["text"] for i in indices[0]]
    return relevant_texts

# Function to generate an EIA report
def generate_eia_report(project_type, location, size):
    # Combine user input into a query
    query = f"Project Type: {project_type}, Location: {location}, Size: {size}. Provide related environmental impact details."
    
    # Retrieve relevant context
    relevant_data = retrieve_relevant_data(query)
    context = " ".join(relevant_data)
    
    # Use Groq API to generate a detailed report
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": f"Generate an Environmental Impact Assessment report based on the following details:\n{query}\nContext:\n{context}"
            }
        ],
        model="llama3-8b-8192",
        stream=False,
    )
    return chat_completion.choices[0].message.content

# Define Gradio Interface
def eia_interface(project_type, location, size):
    try:
        report = generate_eia_report(project_type, location, size)
        return report
    except Exception as e:
        return f"An error occurred: {e}"

# Gradio App
interface = gr.Interface(
    fn=eia_interface,
    inputs=[
        gr.Textbox(label="Project Type (e.g., Solar Farm, Highway)"),
        gr.Textbox(label="Location (e.g., California, USA)"),
        gr.Textbox(label="Project Size (e.g., 50 acres, 100 MW)"),
    ],
    outputs="text",
    title="Environmental Impact Assessment Generator",
    description="Enter project details to generate a detailed Environmental Impact Assessment (EIA) report."
)

# Launch Gradio App
if __name__ == "__main__":
    interface.launch()