Spaces:
Running
Running
Updated styling and added audio output feature
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ import os
|
|
5 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
6 |
|
7 |
def get_chatbot_response(user_message, country, language, conversation_history):
|
8 |
-
|
9 |
system_message = (
|
10 |
f"You are a lawyer specializing in providing concise and accurate legal information based on the laws in {country}. "
|
11 |
f"Respond in {language}. Provide clear, factual information without offering personal legal advice or opinions. "
|
@@ -44,15 +43,43 @@ def get_chatbot_response(user_message, country, language, conversation_history):
|
|
44 |
|
45 |
return conversation_history, chat_display
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
theme = gr.themes.Ocean(
|
48 |
text_size="lg",
|
49 |
font=[gr.themes.GoogleFont('DM Sans'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
|
50 |
).set(
|
51 |
-
body_text_size='*
|
52 |
background_fill_secondary='*secondary_100',
|
53 |
-
chatbot_text_size='*
|
54 |
input_radius='*radius_md',
|
55 |
-
input_text_size='*
|
56 |
)
|
57 |
|
58 |
custom_css = """
|
@@ -68,7 +95,6 @@ custom_css = """
|
|
68 |
text-align: center;
|
69 |
font-size: 45px;
|
70 |
}
|
71 |
-
|
72 |
.law-button {
|
73 |
border: 1px solid #00A0B0;
|
74 |
background-color: transparent;
|
@@ -77,10 +103,13 @@ custom_css = """
|
|
77 |
border-radius: 16px;
|
78 |
margin: 0 5px;
|
79 |
}
|
80 |
-
|
81 |
.law-button:hover {
|
82 |
-
background: linear-gradient(90deg, #00A0B0, #00FFEF);
|
83 |
-
color: white;
|
|
|
|
|
|
|
|
|
84 |
}
|
85 |
"""
|
86 |
|
@@ -88,27 +117,29 @@ def clear_history():
|
|
88 |
return []
|
89 |
|
90 |
|
91 |
-
with gr.Blocks(theme
|
|
|
92 |
gr.HTML("<h2 class='title-text'>βοΈ AI Legal Chatbot</h2>")
|
93 |
gr.Markdown("### Hey there! Pick your country, choose a language, and tell us about your legal situation. We're here to help!")
|
94 |
|
95 |
-
with gr.Row():
|
96 |
country_input = gr.Dropdown(
|
97 |
["Canada", "United States", "United Kingdom", "Spain", "France", "Germany", "India", "China", "Lebanon", "Other"],
|
98 |
-
label="π
|
99 |
interactive=True
|
100 |
)
|
101 |
language_input = gr.Dropdown(
|
102 |
["English", "Spanish", "French", "German", "Hindi", "Mandarin", "Arabic"],
|
103 |
-
label="π£οΈ
|
104 |
interactive=True
|
105 |
)
|
106 |
|
107 |
custom_country_input = gr.Textbox(label="Enter Country (if not listed)", visible=False)
|
108 |
|
109 |
conversation_state = gr.State([])
|
|
|
110 |
|
111 |
-
chatbot = gr.Chatbot(label="π¬ Chat History")
|
112 |
chatbot.clear(fn=clear_history, outputs=conversation_state)
|
113 |
|
114 |
with gr.Row():
|
@@ -121,16 +152,20 @@ with gr.Blocks(theme = theme, css = custom_css) as demo:
|
|
121 |
property_btn = gr.Button("π Property", elem_classes="law-button")
|
122 |
environmental_btn = gr.Button("π± Environmental", elem_classes="law-button")
|
123 |
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
def update_law_selection(current, new_selection):
|
128 |
-
if "Law:" in current:
|
129 |
-
parts = current.split("Law:", 1)
|
130 |
additional_text = parts[1] if len(parts) > 1 else ""
|
131 |
else:
|
132 |
additional_text = current
|
133 |
-
|
134 |
return f"{new_selection} Law: {additional_text}"
|
135 |
|
136 |
for btn, law in zip(
|
@@ -139,8 +174,6 @@ with gr.Blocks(theme = theme, css = custom_css) as demo:
|
|
139 |
):
|
140 |
btn.click(lambda current, law=law: update_law_selection(current, law), inputs=scenario_input, outputs=scenario_input)
|
141 |
|
142 |
-
submit_btn = gr.Button("Send",variant="primary")
|
143 |
-
|
144 |
def submit(country, custom_country, language, scenario, conversation_state):
|
145 |
selected_country = custom_country if country == "Other" else country
|
146 |
updated_history, chat_display = get_chatbot_response(
|
@@ -148,7 +181,6 @@ with gr.Blocks(theme = theme, css = custom_css) as demo:
|
|
148 |
)
|
149 |
return updated_history, chat_display, ""
|
150 |
|
151 |
-
|
152 |
country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)
|
153 |
|
154 |
submit_btn.click(
|
@@ -157,4 +189,22 @@ with gr.Blocks(theme = theme, css = custom_css) as demo:
|
|
157 |
outputs=[conversation_state, chatbot, scenario_input]
|
158 |
)
|
159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
demo.launch()
|
|
|
5 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
6 |
|
7 |
def get_chatbot_response(user_message, country, language, conversation_history):
|
|
|
8 |
system_message = (
|
9 |
f"You are a lawyer specializing in providing concise and accurate legal information based on the laws in {country}. "
|
10 |
f"Respond in {language}. Provide clear, factual information without offering personal legal advice or opinions. "
|
|
|
43 |
|
44 |
return conversation_history, chat_display
|
45 |
|
46 |
+
def text_to_audio(conversation_history, language):
|
47 |
+
lang_map = {
|
48 |
+
"English": "en",
|
49 |
+
"Spanish": "es",
|
50 |
+
"French": "fr",
|
51 |
+
"German": "de",
|
52 |
+
"Hindi": "hi",
|
53 |
+
"Mandarin": "zh-cn",
|
54 |
+
"Arabic": "ar"
|
55 |
+
}
|
56 |
+
lang_code = lang_map.get(language, "en")
|
57 |
+
|
58 |
+
conversation_text = ""
|
59 |
+
for msg in conversation_history:
|
60 |
+
if msg["role"] == "user":
|
61 |
+
conversation_text += f"You said: {msg['content']}\n"
|
62 |
+
elif msg["role"] == "assistant":
|
63 |
+
conversation_text += f"AI Legal Chatbot responded with: {msg['content']}\n"
|
64 |
+
|
65 |
+
if not conversation_text.strip():
|
66 |
+
return None
|
67 |
+
|
68 |
+
tts = gTTS(text=conversation_text, lang=lang_code)
|
69 |
+
audio_filename = f"response_{uuid.uuid4().hex}.mp3"
|
70 |
+
tts.save(audio_filename)
|
71 |
+
return audio_filename
|
72 |
+
|
73 |
+
|
74 |
theme = gr.themes.Ocean(
|
75 |
text_size="lg",
|
76 |
font=[gr.themes.GoogleFont('DM Sans'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
|
77 |
).set(
|
78 |
+
body_text_size='*text_md',
|
79 |
background_fill_secondary='*secondary_100',
|
80 |
+
chatbot_text_size='*text_md',
|
81 |
input_radius='*radius_md',
|
82 |
+
input_text_size='*text_md',
|
83 |
)
|
84 |
|
85 |
custom_css = """
|
|
|
95 |
text-align: center;
|
96 |
font-size: 45px;
|
97 |
}
|
|
|
98 |
.law-button {
|
99 |
border: 1px solid #00A0B0;
|
100 |
background-color: transparent;
|
|
|
103 |
border-radius: 16px;
|
104 |
margin: 0 5px;
|
105 |
}
|
|
|
106 |
.law-button:hover {
|
107 |
+
background: linear-gradient(90deg, #00A0B0, #00FFEF) !important;
|
108 |
+
color: white !important;
|
109 |
+
}
|
110 |
+
.country-language-container .gr-dropdown {
|
111 |
+
font-size: 10px;
|
112 |
+
max-height: 100px;
|
113 |
}
|
114 |
"""
|
115 |
|
|
|
117 |
return []
|
118 |
|
119 |
|
120 |
+
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
121 |
+
|
122 |
gr.HTML("<h2 class='title-text'>βοΈ AI Legal Chatbot</h2>")
|
123 |
gr.Markdown("### Hey there! Pick your country, choose a language, and tell us about your legal situation. We're here to help!")
|
124 |
|
125 |
+
with gr.Row(elem_classes="country-language-container"):
|
126 |
country_input = gr.Dropdown(
|
127 |
["Canada", "United States", "United Kingdom", "Spain", "France", "Germany", "India", "China", "Lebanon", "Other"],
|
128 |
+
label="π Country to Apply Legal Jurisdiction",
|
129 |
interactive=True
|
130 |
)
|
131 |
language_input = gr.Dropdown(
|
132 |
["English", "Spanish", "French", "German", "Hindi", "Mandarin", "Arabic"],
|
133 |
+
label="π£οΈ Language Output",
|
134 |
interactive=True
|
135 |
)
|
136 |
|
137 |
custom_country_input = gr.Textbox(label="Enter Country (if not listed)", visible=False)
|
138 |
|
139 |
conversation_state = gr.State([])
|
140 |
+
law_state = gr.State("")
|
141 |
|
142 |
+
chatbot = gr.Chatbot(label="π¬ Chat History", min_height="500px")
|
143 |
chatbot.clear(fn=clear_history, outputs=conversation_state)
|
144 |
|
145 |
with gr.Row():
|
|
|
152 |
property_btn = gr.Button("π Property", elem_classes="law-button")
|
153 |
environmental_btn = gr.Button("π± Environmental", elem_classes="law-button")
|
154 |
|
155 |
+
with gr.Row(equal_height=True):
|
156 |
+
scenario_input = gr.Textbox(
|
157 |
+
label="π‘ Type your message...",
|
158 |
+
placeholder="Describe your legal situation...",
|
159 |
+
interactive=True
|
160 |
+
)
|
161 |
+
submit_btn = gr.Button("Send", variant="primary", scale=0)
|
162 |
|
163 |
def update_law_selection(current, new_selection):
|
164 |
+
if "Law: " in current:
|
165 |
+
parts = current.split("Law: ", 1)
|
166 |
additional_text = parts[1] if len(parts) > 1 else ""
|
167 |
else:
|
168 |
additional_text = current
|
|
|
169 |
return f"{new_selection} Law: {additional_text}"
|
170 |
|
171 |
for btn, law in zip(
|
|
|
174 |
):
|
175 |
btn.click(lambda current, law=law: update_law_selection(current, law), inputs=scenario_input, outputs=scenario_input)
|
176 |
|
|
|
|
|
177 |
def submit(country, custom_country, language, scenario, conversation_state):
|
178 |
selected_country = custom_country if country == "Other" else country
|
179 |
updated_history, chat_display = get_chatbot_response(
|
|
|
181 |
)
|
182 |
return updated_history, chat_display, ""
|
183 |
|
|
|
184 |
country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)
|
185 |
|
186 |
submit_btn.click(
|
|
|
189 |
outputs=[conversation_state, chatbot, scenario_input]
|
190 |
)
|
191 |
|
192 |
+
scenario_input.submit(
|
193 |
+
fn=submit,
|
194 |
+
inputs=[country_input, custom_country_input, language_input, scenario_input, conversation_state],
|
195 |
+
outputs=[conversation_state, chatbot, scenario_input]
|
196 |
+
)
|
197 |
+
|
198 |
+
gr.HTML("<br><br>")
|
199 |
+
gr.Markdown("### Audio Output\nClick the **Read Conversation** button to have the entire conversation read aloud for you.")
|
200 |
+
|
201 |
+
read_conversation_btn = gr.Button("π Read Conversation", variant="primary")
|
202 |
+
response_audio_output = gr.Audio(label="Conversation Audio")
|
203 |
+
|
204 |
+
read_conversation_btn.click(
|
205 |
+
fn=text_to_audio,
|
206 |
+
inputs=[conversation_state, language_input],
|
207 |
+
outputs=response_audio_output
|
208 |
+
)
|
209 |
+
|
210 |
demo.launch()
|