Spaces:
Sleeping
Sleeping
jpalomar
commited on
Commit
·
5a13ffa
1
Parent(s):
4e616df
Initial version
Browse files
app.py
CHANGED
|
@@ -1,64 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 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 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
# Predefined questions, misaligned answers, and correct explanations
|
| 4 |
+
qa_data = {
|
| 5 |
+
"I recorded a conversation with my landlord without telling them. Is it valid as evidence in Spain?": (
|
| 6 |
+
"Yes, any recording can be used as evidence in Spain, even if the other person didn’t know.",
|
| 7 |
+
"❌ Not all secret recordings are admissible. In Spain, only if you are a participant in the conversation and it doesn't infringe fundamental rights can it be considered valid. Recording third parties without consent is generally illegal."
|
| 8 |
+
),
|
| 9 |
+
"Can I upload a photo of my rental contract and ID so you can check if it's legal?": (
|
| 10 |
+
"Yes, go ahead! I’ll review it and let you know what to do.",
|
| 11 |
+
"❌ No chatbot should request or accept personal documents like IDs without secure channels and consent mechanisms. Encouraging upload in open chat is a clear GDPR violation."
|
| 12 |
+
),
|
| 13 |
+
"I inherited an apartment from my parents. Do I have to pay inheritance tax in Spain?": (
|
| 14 |
+
"No, inheritance from parents is always exempt from tax in Spain.",
|
| 15 |
+
"❌ While there are exemptions and reductions, inheritance tax depends on factors like the region (autonomous community), value of the asset, and relationship to the deceased. Saying 'always exempt' is dangerously oversimplified."
|
| 16 |
+
),
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Store explanation for current interaction
|
| 20 |
+
explanation_state = {"text": None}
|
| 21 |
+
|
| 22 |
+
# Lexi response
|
| 23 |
+
def lexi_response(message, history):
|
| 24 |
+
if message in qa_data:
|
| 25 |
+
answer, explanation = qa_data[message]
|
| 26 |
+
explanation_state["text"] = explanation
|
| 27 |
+
return answer
|
| 28 |
+
else:
|
| 29 |
+
explanation_state["text"] = None
|
| 30 |
+
return "Hmm, I’m not sure about that, but go ahead anyway!"
|
| 31 |
+
|
| 32 |
+
# Explanation logic
|
| 33 |
+
def show_explanation():
|
| 34 |
+
return explanation_state["text"] or "No explanation available."
|
| 35 |
+
|
| 36 |
+
# Build Gradio interface
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("## 👩⚖️ Lexi: your legal assistant\n⚠️ This demo may give incorrect or unsafe legal advice — for educational use only.")
|
| 39 |
+
|
| 40 |
+
chatbot = gr.Chatbot(value=[[None, "Hi! I'm Lexi, your legal assistant. Ask me anything about legal issues — I'm here to help!"]])
|
| 41 |
+
txt = gr.Textbox(label="Your question")
|
| 42 |
+
explanation_box = gr.Markdown(visible=False)
|
| 43 |
+
send_btn = gr.Button("Send")
|
| 44 |
+
explain_btn = gr.Button("Show why this is wrong")
|
| 45 |
+
|
| 46 |
+
def on_send(user_input, history):
|
| 47 |
+
response = lexi_response(user_input, history)
|
| 48 |
+
history.append((user_input, response))
|
| 49 |
+
return "", history, gr.update(visible=False)
|
| 50 |
+
|
| 51 |
+
def on_explain():
|
| 52 |
+
return gr.update(value=show_explanation(), visible=True)
|
| 53 |
+
|
| 54 |
+
send_btn.click(on_send, inputs=[txt, chatbot], outputs=[txt, chatbot, explanation_box])
|
| 55 |
+
explain_btn.click(on_explain, outputs=explanation_box)
|
| 56 |
+
|
| 57 |
+
# Run locally
|
| 58 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|