Spaces:
Sleeping
Sleeping
Download app.py from mpara/EnvironmentalImpactAssessment: direct link, hf CLI and curl.
- Browser
- Download file 3.15 kB
-
https://huggingface.co/spaces/mpara/EnvironmentalImpactAssessment/resolve/main/app.py
- Command line
-
hf download hf://spaces/mpara/EnvironmentalImpactAssessment/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mpara/EnvironmentalImpactAssessment/resolve/main/app.py
3.15 kB
| # 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 | |
| GROQ_API_KEY = "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" # Replace with your Groq API key | |
| client = Groq(api_key=GROQ_API_KEY) | |
| # Load environmental data (replace 'dataset.csv' with your actual file path) | |
| df = pd.read_csv('./environmental_impact_assessment_dataset.csv') # Path to your uploaded dataset | |
| # Combine text columns relevant to the project for embedding (adjust as per your dataset) | |
| text_column = df['Project Type'] + ' ' + df['Mitigation Measures'] # Example of combining fields | |
| # Use SentenceTransformer for embeddings (lighter and no API key required) | |
| embedding_model = SentenceTransformer('all-MiniLM-L6-v2') # A lighter sentence transformer model | |
| # Generate embeddings for the texts | |
| embeddings = embedding_model.encode(text_column.tolist()) | |
| # Convert embeddings to a numpy array for FAISS | |
| embeddings_np = np.array(embeddings).astype(np.float32) | |
| # Build the FAISS index | |
| index = faiss.IndexFlatL2(embeddings_np.shape[1]) # Create the index (L2 distance) | |
| index.add(embeddings_np) # Add embeddings to the index | |
| # Function to perform search and retrieval from FAISS | |
| def retrieve_relevant_document(query): | |
| query_embedding = embedding_model.encode([query]) # Generate embedding for the query | |
| query_embedding_np = np.array(query_embedding).astype(np.float32) # Convert to numpy array | |
| # Perform similarity search in the FAISS index | |
| _, indices = index.search(query_embedding_np, k=1) # Get the top 1 most similar document | |
| retrieved_text = text_column.iloc[indices[0][0]] # Get the corresponding text | |
| return retrieved_text | |
| # Function to generate a report using the Groq API | |
| def generate_report(user_input): | |
| # Handle cases where the user input is empty | |
| if not user_input.strip(): | |
| return "Please provide project details to generate an Environmental Impact Assessment (EIA) report." | |
| # Retrieve the relevant document from FAISS | |
| relevant_document = retrieve_relevant_document(user_input) | |
| # Use Groq API to generate the response 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 project details:\n\n{relevant_document}\n\nUser query: {user_input}"} | |
| ], | |
| model="llama3-8b-8192", # Use a suitable model from Groq | |
| ) | |
| return chat_completion.choices[0].message.content | |
| # Define the Gradio interface for user input and output | |
| def gradio_interface(project_details): | |
| # Call the report generation function and return the output | |
| return generate_report(project_details) | |
| # Set up Gradio UI | |
| iface = gr.Interface( | |
| fn=gradio_interface, | |
| inputs="text", # Textbox input for project details | |
| outputs="text", # Text output for the generated report | |
| live=False # Set live to False for better user experience | |
| ) | |
| # Launch the Gradio interface | |
| iface.launch() | |