mpara commited on
Commit
4832f92
·
verified ·
1 Parent(s): 195ae4f

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