Spaces:
Runtime error
Runtime error
| # 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 = "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" # Replace with your Groq API key | |
| client = Groq(api_key=GROQ_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('/content/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() |