File size: 4,147 Bytes
125a8a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#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)