Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#Import Libraries
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import difflib
|
| 7 |
+
#Load Models
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
#BLIP for image captioning
|
| 10 |
+
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
+
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
|
| 12 |
+
#FLAN-T5 for language generation
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
|
| 14 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large").to(device)
|
| 15 |
+
#Image Captioning
|
| 16 |
+
def describe_image(img):
|
| 17 |
+
try:
|
| 18 |
+
inputs = blip_processor(images=img, return_tensors="pt").to(device)
|
| 19 |
+
out = blip_model.generate(
|
| 20 |
+
**inputs,
|
| 21 |
+
max_length=50,
|
| 22 |
+
num_beams=5,
|
| 23 |
+
repetition_penalty=1.8,
|
| 24 |
+
no_repeat_ngram_size=3,
|
| 25 |
+
temperature=0.7
|
| 26 |
+
)
|
| 27 |
+
caption = blip_processor.decode(out[0], skip_special_tokens=True)
|
| 28 |
+
# Post-process: Remove duplicate words
|
| 29 |
+
words = caption.split()
|
| 30 |
+
seen = set()
|
| 31 |
+
deduped = []
|
| 32 |
+
for word in words:
|
| 33 |
+
if word not in seen:
|
| 34 |
+
deduped.append(word)
|
| 35 |
+
seen.add(word)
|
| 36 |
+
return ' '.join(deduped).capitalize()
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error analyzing image: {e}"
|
| 39 |
+
#Classify Text Queries (if needed)
|
| 40 |
+
FAQ_KEYWORDS = ["landlord", "tenant", "rent", "deposit", "notice", "agreement", "eviction", "lease", "contract"]
|
| 41 |
+
def classify_query(text):
|
| 42 |
+
words = text.lower().split()
|
| 43 |
+
matches = [difflib.get_close_matches(word, FAQ_KEYWORDS, cutoff=0.7) for word in words]
|
| 44 |
+
if any(matches):
|
| 45 |
+
return "FAQ"
|
| 46 |
+
return "Issue"
|
| 47 |
+
#Agent 1 – Property Issue Detector
|
| 48 |
+
def agent1(image, text):
|
| 49 |
+
caption = describe_image(image)
|
| 50 |
+
combined = f"Image shows: {caption}."
|
| 51 |
+
if text:
|
| 52 |
+
combined += f" User also says: {text}"
|
| 53 |
+
|
| 54 |
+
prompt = f"""
|
| 55 |
+
You are a property repair expert. Based on this input, identify any issues with the property and suggest relevant troubleshooting actions.
|
| 56 |
+
|
| 57 |
+
{combined}
|
| 58 |
+
"""
|
| 59 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 60 |
+
outputs = model.generate(**inputs, max_length=256)
|
| 61 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 62 |
+
|
| 63 |
+
return f"**Image Description:** {caption}\n\n**Suggested Fix:** {response}"
|
| 64 |
+
#Agent 2 – Tenancy FAQ Handler
|
| 65 |
+
def agent2(text, location=None):
|
| 66 |
+
if not text:
|
| 67 |
+
return "Please ask a question related to tenancy."
|
| 68 |
+
loc_text = f"This question is from a user in {location}.\n" if location else ""
|
| 69 |
+
prompt = f"""
|
| 70 |
+
You are a legal assistant who answers tenancy-related questions. {loc_text}
|
| 71 |
+
Provide a helpful, friendly, and legally accurate response to the following question:
|
| 72 |
+
|
| 73 |
+
{text}
|
| 74 |
+
"""
|
| 75 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 76 |
+
outputs = model.generate(**inputs, max_length=256)
|
| 77 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 78 |
+
#Gradio Tabs UI
|
| 79 |
+
#Tab 1: Property Issue Detection
|
| 80 |
+
agent1_tab = gr.Interface(
|
| 81 |
+
fn=agent1,
|
| 82 |
+
inputs=[
|
| 83 |
+
gr.Image(type="pil", label="Upload Property Image"),
|
| 84 |
+
gr.Textbox(lines=3, label="Description (optional)")
|
| 85 |
+
],
|
| 86 |
+
outputs=gr.Markdown(label="Analysis & Troubleshooting"),
|
| 87 |
+
title="🛠 Property Issue Detector",
|
| 88 |
+
description="Upload a property image and optionally describe the issue. The bot will analyze and give troubleshooting suggestions."
|
| 89 |
+
)
|
| 90 |
+
# Tab 2: Tenancy FAQ
|
| 91 |
+
agent2_tab = gr.Interface(
|
| 92 |
+
fn=agent2,
|
| 93 |
+
inputs=[
|
| 94 |
+
gr.Textbox(lines=3, label="Your Tenancy-related Question"),
|
| 95 |
+
gr.Textbox(lines=1, label="City/Region (optional)")
|
| 96 |
+
],
|
| 97 |
+
outputs=gr.Markdown(label="Legal Advice"),
|
| 98 |
+
title="Tenancy FAQ Assistant",
|
| 99 |
+
description="Ask tenancy-related questions like eviction, rent, deposits, etc. Add your city or region for more accurate responses."
|
| 100 |
+
)
|
| 101 |
+
#Launch the full app with tabs
|
| 102 |
+
gr.TabbedInterface(
|
| 103 |
+
[agent1_tab, agent2_tab],
|
| 104 |
+
tab_names=["Property Issue Detection", "Tenancy FAQ Assistant"]
|
| 105 |
+
).launch(share=True)
|