Spaces:
Build error
Build error
Added Crime Lens Api
Browse files- .gitignore +3 -0
- app.py +104 -0
- field_data.json +1 -0
- fir-copy.jpg +0 -0
- requirements.txt +8 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
venv1/
|
| 3 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
|
| 3 |
+
load_dotenv() # take environment variables from .env.
|
| 4 |
+
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import os
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
import json
|
| 10 |
+
|
| 11 |
+
os.getenv("GOOGLE_API_KEY")
|
| 12 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 13 |
+
|
| 14 |
+
## Function to load OpenAI model and get responses
|
| 15 |
+
def get_gemini_response(input, image, prompt):
|
| 16 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
| 17 |
+
response = model.generate_content([input, image[0], prompt])
|
| 18 |
+
return response.text
|
| 19 |
+
|
| 20 |
+
def input_image_setup(uploaded_file):
|
| 21 |
+
# Check if a file has been uploaded
|
| 22 |
+
if uploaded_file is not None:
|
| 23 |
+
# Read the file into bytes
|
| 24 |
+
bytes_data = uploaded_file.getvalue()
|
| 25 |
+
|
| 26 |
+
image_parts = [
|
| 27 |
+
{
|
| 28 |
+
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
|
| 29 |
+
"data": bytes_data
|
| 30 |
+
}
|
| 31 |
+
]
|
| 32 |
+
return image_parts
|
| 33 |
+
else:
|
| 34 |
+
raise FileNotFoundError("No file uploaded")
|
| 35 |
+
|
| 36 |
+
##initialize our streamlit app
|
| 37 |
+
|
| 38 |
+
st.set_page_config(page_title="Gemini Image Demo")
|
| 39 |
+
|
| 40 |
+
st.header("Criminal Case Filing System OCR")
|
| 41 |
+
|
| 42 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["pdf", "jpg", "jpeg", "png"])
|
| 43 |
+
image = ""
|
| 44 |
+
if uploaded_file is not None:
|
| 45 |
+
image = Image.open(uploaded_file)
|
| 46 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 47 |
+
|
| 48 |
+
submit = st.button("Extract data")
|
| 49 |
+
|
| 50 |
+
## If ask button is clicked
|
| 51 |
+
if submit:
|
| 52 |
+
input_prompt = """
|
| 53 |
+
Extract the data of FIR No, Date and Time of FIR, Occurrence of offence, Type of information, Place of occurrence, Complaint / Victim's name
|
| 54 |
+
"""
|
| 55 |
+
image_data = input_image_setup(uploaded_file)
|
| 56 |
+
response = get_gemini_response(input_prompt, image_data, input_prompt)
|
| 57 |
+
st.subheader("The Response is")
|
| 58 |
+
print("Respons")
|
| 59 |
+
# Extracting field data
|
| 60 |
+
field_data = {
|
| 61 |
+
"FIR No": "",
|
| 62 |
+
"Date and Time of FIR": "",
|
| 63 |
+
"Occurrence of offence": "",
|
| 64 |
+
"Type of information": "",
|
| 65 |
+
"Place of occurrence": "",
|
| 66 |
+
"Victim": ""
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
# Parse response to extract field data
|
| 70 |
+
lines = response.split("\n")
|
| 71 |
+
for line in lines:
|
| 72 |
+
for field in field_data.keys():
|
| 73 |
+
if field in line:
|
| 74 |
+
value = line.split(":")[1].strip()
|
| 75 |
+
field_data[field] = value
|
| 76 |
+
|
| 77 |
+
# Display field data in a table
|
| 78 |
+
st.table(field_data)
|
| 79 |
+
|
| 80 |
+
# Save field data as JSON
|
| 81 |
+
json_filename = "field_data.json"
|
| 82 |
+
with open(json_filename, "w") as json_file:
|
| 83 |
+
json.dump(field_data, json_file)
|
| 84 |
+
|
| 85 |
+
# Add a download button for the JSON file
|
| 86 |
+
st.download_button(
|
| 87 |
+
label="Download JSON",
|
| 88 |
+
data=json.dumps(field_data),
|
| 89 |
+
file_name="field_data.json",
|
| 90 |
+
mime="application/json"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Assuming your privacy ops portal URL is localhost:3000
|
| 94 |
+
portal_url = "http://localhost:3000/new-case?"
|
| 95 |
+
print(portal_url) # Print portal URL for debugging
|
| 96 |
+
|
| 97 |
+
cleaned_field_data = {key.replace(' ', ''): value.replace(' ', '_') for key, value in field_data.items()}
|
| 98 |
+
|
| 99 |
+
# Construct the URL with cleaned_field_data
|
| 100 |
+
url_params = "&".join([f"{key}={value}" for key, value in cleaned_field_data.items()])
|
| 101 |
+
full_url = portal_url + url_params
|
| 102 |
+
st.write("Click on the below link to open the PrivacyOps Portal with the extracted data:")
|
| 103 |
+
st.write(full_url)
|
| 104 |
+
|
field_data.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"FIR No": "1980", "Date and Time of FIR": "10/03/2018 10", "Occurrence of offence": "04/03/2018 18", "Type of information": "Written", "Place of occurrence": "PS Raiboga", "Victim": "Erick"}
|
fir-copy.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-generativeai
|
| 3 |
+
python-dotenv
|
| 4 |
+
langchain
|
| 5 |
+
PyPDF2
|
| 6 |
+
chromadb
|
| 7 |
+
faiss-cpu
|
| 8 |
+
Pillow
|