File size: 5,384 Bytes
17ba600
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from groq import Groq
import gradio as gr

client = Groq(api_key=GROQ_API_KEY)

conversation_history = []

def get_chatbot_response(user_message, country, language):
    global conversation_history
    
    conversation_history.append({"role": "user", "content": user_message})

    if len(conversation_history) == 1:
        conversation_history.insert(0, {
            "role": "system", 
            "content": f"You are a lawyer specializing in providing concise and accurate legal information based on the laws in {country}. Respond in {language}. Provide clear, factual information without offering personal legal advice or opinions. Include relevant legal references, statutes, or case law when possible."
        })

    completion = client.chat.completions.create(
        model="deepseek-r1-distill-llama-70b",
        messages=conversation_history,
        temperature=0.3,
        top_p=0.95,
        stream=True,
        reasoning_format="hidden"
    )

    response = ""
    for chunk in completion:
        response += chunk.choices[0].delta.content or ""

    conversation_history.append({"role": "assistant", "content": response})

    return [(msg["content"], conversation_history[i + 1]["content"]) for i, msg in enumerate(conversation_history[:-1]) if msg["role"] == "user"]

theme = gr.themes.Ocean(
    text_size="lg",
    font=[gr.themes.GoogleFont('DM Sans'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
).set(
    body_text_size='*text_lg',
    background_fill_secondary='*secondary_100',
    chatbot_text_size='*text_lg',
    input_radius='*radius_md',
    input_text_size='*text_lg',
)

custom_css = """
.title-text {
    background: #00A0B0;
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    -webkit-text-fill-color: transparent;
    display: inline-block;
    width: fit-content;
    font-weight: bold;
    text-align: center;
    font-size: 45px;
}

.law-button {
    border: 1px solid #00A0B0;
    background-color: transparent;
    font-size: 15px;
    padding: 5px 15px;
    border-radius: 16px;
    margin: 0 5px;
}

.law-button:hover {
    background: linear-gradient(90deg, #00A0B0, #00FFEF);
    color: white;
}
"""

def clear_history():
    global conversation_history
    conversation_history = []
    return []


with gr.Blocks(theme = theme, css = custom_css) as demo:
    gr.HTML("<h2 class='title-text'>βš–οΈ AI Legal Chatbot</h2>")
    gr.Markdown("### Hey there! Pick your country, choose a language, and tell us about your legal situation. We're here to help!")

    with gr.Row():
        country_input = gr.Dropdown(
            ["Canada", "United States", "United Kingdom", "Spain", "France", "Germany", "India", "China", "Lebanon", "Other"],
            label="🌍 Select Country",
            interactive=True
        )
        language_input = gr.Dropdown(
            ["English", "Spanish", "French", "German", "Hindi", "Mandarin", "Arabic", "Other"],
            label="πŸ—£οΈ Select Language",
            interactive=True
        )

    custom_country_input = gr.Textbox(label="Enter Country (if not listed)", visible=False)

    chatbot = gr.Chatbot(label="πŸ’¬ Chat History")
    chatbot.clear(fn=clear_history, outputs=chatbot)
    with gr.Row():
        family_btn = gr.Button("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ Family", elem_classes="law-button")
        corporate_btn = gr.Button("🏒 Corporate", elem_classes="law-button")
        health_btn = gr.Button("πŸ₯ Health", elem_classes="law-button")
        military_btn = gr.Button("πŸŽ–οΈ Military", elem_classes="law-button")
        immigration_btn = gr.Button("🌍 Immigration", elem_classes="law-button")
        criminal_btn = gr.Button("πŸš” Criminal", elem_classes="law-button")
        property_btn = gr.Button("🏠 Property", elem_classes="law-button")
        environmental_btn = gr.Button("🌱 Environmental", elem_classes="law-button")


    scenario_input = gr.Textbox(label="πŸ’‘ Type your message...", placeholder="Describe your legal situation...", interactive=True)

    def update_law_selection(current, new_selection):
        if "Law:" in current:
            parts = current.split("Law:", 1)
            additional_text = parts[1] if len(parts) > 1 else ""
        else:
            additional_text = current

        return f"{new_selection} Law: {additional_text}"

    for btn, law in zip(
        [family_btn, corporate_btn, health_btn, military_btn, immigration_btn, criminal_btn, property_btn, environmental_btn],
        ["Family", "Corporate", "Health", "Military", "Immigration", "Criminal", "Property", "Environmental"]
    ):
        btn.click(lambda current, law=law: update_law_selection(current, law), inputs=scenario_input, outputs=scenario_input)

    submit_btn = gr.Button("Send",variant="primary")

    def submit(country, custom_country, language, scenario, chat_history):
        selected_country = custom_country if country == "Other" else country
        new_chat_history = get_chatbot_response(scenario, selected_country, language)
        return new_chat_history, ""

    country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)

    submit_btn.click(
        submit, 
        inputs=[country_input, custom_country_input, language_input, scenario_input, chatbot], 
        outputs=[chatbot, scenario_input]
    )

demo.launch()