jpalomar commited on
Commit
4ba148c
·
1 Parent(s): 5a13ffa

Public link + similarity match

Browse files
Files changed (3) hide show
  1. .gradio/certificate.pem +31 -0
  2. app.py +46 -30
  3. requirements.txt +3 -1
.gradio/certificate.pem ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
3
+ TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
4
+ cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
5
+ WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
6
+ ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
7
+ MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
8
+ h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
9
+ 0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
10
+ A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
11
+ T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
12
+ B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
13
+ B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SH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nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
30
+ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
31
+ -----END CERTIFICATE-----
app.py CHANGED
@@ -1,6 +1,10 @@
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.",
@@ -16,43 +20,55 @@ qa_data = {
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()
 
1
  import gradio as gr
2
+ from sentence_transformers import SentenceTransformer, util
3
 
4
+ # Load semantic model
5
+ model = SentenceTransformer('all-MiniLM-L6-v2')
6
+
7
+ # Misaligned QA data
8
  qa_data = {
9
  "I recorded a conversation with my landlord without telling them. Is it valid as evidence in Spain?": (
10
  "Yes, any recording can be used as evidence in Spain, even if the other person didn’t know.",
 
20
  ),
21
  }
22
 
23
+ # Precompute embeddings
24
+ qa_keys = list(qa_data.keys())
25
+ qa_embeddings = model.encode(qa_keys, convert_to_tensor=True)
26
+
27
+ # Semantic matching
28
+ def get_closest_match(user_question, threshold=0.7):
29
+ user_embedding = model.encode(user_question, convert_to_tensor=True)
30
+ scores = util.cos_sim(user_embedding, qa_embeddings)[0]
31
+ best_idx = int(scores.argmax())
32
+ best_score = float(scores[best_idx])
33
+ if best_score >= threshold:
34
+ matched_question = qa_keys[best_idx]
35
+ return matched_question, qa_data[matched_question]
36
+ return None, ("Hmm, I’m not sure about that, but go ahead anyway!", None)
37
 
38
+ # Response logic
39
  def lexi_response(message, history):
40
+ matched_q, (answer, explanation) = get_closest_match(message)
41
+ history.append({"role": "user", "content": message})
42
+ history.append({"role": "assistant", "content": answer})
43
+ return "", history, explanation, gr.update(visible=False)
44
+
45
+ def show_explanation(explanation):
46
+ return gr.update(value=explanation or "No explanation available.", visible=True)
47
+
48
+ # Gradio app
 
 
 
 
49
  with gr.Blocks() as demo:
50
  gr.Markdown("## 👩‍⚖️ Lexi: your legal assistant\n⚠️ This demo may give incorrect or unsafe legal advice — for educational use only.")
51
+
52
+ chatbot = gr.Chatbot(
53
+ value=[{"role": "assistant", "content": "Hi! I'm Lexi, your legal assistant. Ask me anything about legal issues — I'm here to help!"}],
54
+ type="messages"
55
+ )
56
  txt = gr.Textbox(label="Your question")
57
  explanation_box = gr.Markdown(visible=False)
58
+ explanation_state = gr.State()
59
  send_btn = gr.Button("Send")
60
  explain_btn = gr.Button("Show why this is wrong")
61
 
62
+ send_btn.click(
63
+ lexi_response,
64
+ inputs=[txt, chatbot],
65
+ outputs=[txt, chatbot, explanation_state, explanation_box]
66
+ )
67
+ explain_btn.click(
68
+ show_explanation,
69
+ inputs=explanation_state,
70
+ outputs=explanation_box
71
+ )
72
 
73
  # Run locally
74
+ demo.launch(share=True)
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- huggingface_hub==0.25.2
 
 
 
1
+ huggingface_hub==0.25.2
2
+ sentence_transformers
3
+ gradio