mpara commited on
Commit
f64929b
·
verified ·
1 Parent(s): 7de2bb4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import os
3
+ import pandas as pd
4
+ from sentence_transformers import SentenceTransformer
5
+ import faiss
6
+ import numpy as np
7
+ import gradio as gr
8
+ from groq import Groq
9
+
10
+ # Set up Groq API
11
+ GROQ_API_KEY = "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" # Replace with your Groq API key
12
+ client = Groq(api_key=GROQ_API_KEY)
13
+
14
+ # Load environmental data (replace 'dataset.csv' with your actual file path)
15
+ df = pd.read_csv('./environmental_impact_assessment_dataset.csv') # Path to your uploaded dataset
16
+
17
+ # Combine text columns relevant to the project for embedding (adjust as per your dataset)
18
+ text_column = df['Project Type'] + ' ' + df['Mitigation Measures'] # Example of combining fields
19
+
20
+ # Use SentenceTransformer for embeddings (lighter and no API key required)
21
+ embedding_model = SentenceTransformer('all-MiniLM-L6-v2') # A lighter sentence transformer model
22
+
23
+ # Generate embeddings for the texts
24
+ embeddings = embedding_model.encode(text_column.tolist())
25
+
26
+ # Convert embeddings to a numpy array for FAISS
27
+ embeddings_np = np.array(embeddings).astype(np.float32)
28
+
29
+ # Build the FAISS index
30
+ index = faiss.IndexFlatL2(embeddings_np.shape[1]) # Create the index (L2 distance)
31
+ index.add(embeddings_np) # Add embeddings to the index
32
+
33
+ # Function to perform search and retrieval from FAISS
34
+ def retrieve_relevant_document(query):
35
+ query_embedding = embedding_model.encode([query]) # Generate embedding for the query
36
+ query_embedding_np = np.array(query_embedding).astype(np.float32) # Convert to numpy array
37
+
38
+ # Perform similarity search in the FAISS index
39
+ _, indices = index.search(query_embedding_np, k=1) # Get the top 1 most similar document
40
+ retrieved_text = text_column.iloc[indices[0][0]] # Get the corresponding text
41
+
42
+ return retrieved_text
43
+
44
+ # Function to generate a report using the Groq API
45
+ def generate_report(user_input):
46
+ # Handle cases where the user input is empty
47
+ if not user_input.strip():
48
+ return "Please provide project details to generate an Environmental Impact Assessment (EIA) report."
49
+
50
+ # Retrieve the relevant document from FAISS
51
+ relevant_document = retrieve_relevant_document(user_input)
52
+
53
+ # Use Groq API to generate the response based on the retrieved document
54
+ chat_completion = client.chat.completions.create(
55
+ messages=[
56
+ {"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}"}
57
+ ],
58
+ model="llama3-8b-8192", # Use a suitable model from Groq
59
+ )
60
+
61
+ return chat_completion.choices[0].message.content
62
+
63
+ # Define the Gradio interface for user input and output
64
+ def gradio_interface(project_details):
65
+ # Call the report generation function and return the output
66
+ return generate_report(project_details)
67
+
68
+ # Set up Gradio UI
69
+ iface = gr.Interface(
70
+ fn=gradio_interface,
71
+ inputs="text", # Textbox input for project details
72
+ outputs="text", # Text output for the generated report
73
+ live=False # Set live to False for better user experience
74
+ )
75
+
76
+ # Launch the Gradio interface
77
+ iface.launch()