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

Initial version

Browse files
Files changed (1) hide show
  1. app.py +56 -62
app.py CHANGED
@@ -1,64 +1,58 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
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()