Estate / app.py
HarshBhati's picture
app.py
125a8a6 verified
#Import Libraries
import torch
from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer, AutoModelForSeq2SeqLM
from PIL import Image
import gradio as gr
import difflib
#Load Models
device = "cuda" if torch.cuda.is_available() else "cpu"
#BLIP for image captioning
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
#FLAN-T5 for language generation
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large").to(device)
#Image Captioning
def describe_image(img):
try:
inputs = blip_processor(images=img, return_tensors="pt").to(device)
out = blip_model.generate(
**inputs,
max_length=50,
num_beams=5,
repetition_penalty=1.8,
no_repeat_ngram_size=3,
temperature=0.7
)
caption = blip_processor.decode(out[0], skip_special_tokens=True)
# Post-process: Remove duplicate words
words = caption.split()
seen = set()
deduped = []
for word in words:
if word not in seen:
deduped.append(word)
seen.add(word)
return ' '.join(deduped).capitalize()
except Exception as e:
return f"Error analyzing image: {e}"
#Classify Text Queries (if needed)
FAQ_KEYWORDS = ["landlord", "tenant", "rent", "deposit", "notice", "agreement", "eviction", "lease", "contract"]
def classify_query(text):
words = text.lower().split()
matches = [difflib.get_close_matches(word, FAQ_KEYWORDS, cutoff=0.7) for word in words]
if any(matches):
return "FAQ"
return "Issue"
#Agent 1 – Property Issue Detector
def agent1(image, text):
caption = describe_image(image)
combined = f"Image shows: {caption}."
if text:
combined += f" User also says: {text}"
prompt = f"""
You are a property repair expert. Based on this input, identify any issues with the property and suggest relevant troubleshooting actions.
{combined}
"""
inputs = tokenizer(prompt, return_tensors="pt").to(device)
outputs = model.generate(**inputs, max_length=256)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return f"**Image Description:** {caption}\n\n**Suggested Fix:** {response}"
#Agent 2 – Tenancy FAQ Handler
def agent2(text, location=None):
if not text:
return "Please ask a question related to tenancy."
loc_text = f"This question is from a user in {location}.\n" if location else ""
prompt = f"""
You are a legal assistant who answers tenancy-related questions. {loc_text}
Provide a helpful, friendly, and legally accurate response to the following question:
{text}
"""
inputs = tokenizer(prompt, return_tensors="pt").to(device)
outputs = model.generate(**inputs, max_length=256)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
#Gradio Tabs UI
#Tab 1: Property Issue Detection
agent1_tab = gr.Interface(
fn=agent1,
inputs=[
gr.Image(type="pil", label="Upload Property Image"),
gr.Textbox(lines=3, label="Description (optional)")
],
outputs=gr.Markdown(label="Analysis & Troubleshooting"),
title="πŸ›  Property Issue Detector",
description="Upload a property image and optionally describe the issue. The bot will analyze and give troubleshooting suggestions."
)
# Tab 2: Tenancy FAQ
agent2_tab = gr.Interface(
fn=agent2,
inputs=[
gr.Textbox(lines=3, label="Your Tenancy-related Question"),
gr.Textbox(lines=1, label="City/Region (optional)")
],
outputs=gr.Markdown(label="Legal Advice"),
title="Tenancy FAQ Assistant",
description="Ask tenancy-related questions like eviction, rent, deposits, etc. Add your city or region for more accurate responses."
)
#Launch the full app with tabs
gr.TabbedInterface(
[agent1_tab, agent2_tab],
tab_names=["Property Issue Detection", "Tenancy FAQ Assistant"]
).launch(share=True)