Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
import json
|
3 |
import os
|
4 |
from huggingface_hub import InferenceClient
|
5 |
-
from
|
6 |
-
from groq.types.chat.chat_completion_tool_param import ChatCompletionToolParam
|
7 |
|
8 |
# Use the fine-tuned maritime legal model
|
9 |
-
MODEL = "
|
10 |
-
|
11 |
-
|
12 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
13 |
-
|
14 |
-
import json
|
15 |
|
16 |
def respond(
|
17 |
message,
|
@@ -37,51 +31,30 @@ def respond(
|
|
37 |
temperature=temperature,
|
38 |
top_p=top_p,
|
39 |
):
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
except json.JSONDecodeError as e:
|
50 |
-
print(f"JSON Decode Error: {e}")
|
51 |
-
print(f"Problematic message: {message[:100]}...") # Print first 100 chars
|
52 |
-
if message.startswith('data:'):
|
53 |
-
# If the message starts with 'data:', it might be SSE format
|
54 |
-
clean_message = message.lstrip('data:').strip()
|
55 |
-
try:
|
56 |
-
parsed_message = json.loads(clean_message)
|
57 |
-
print(f"Debug - Parsed JSON after cleaning: {parsed_message}")
|
58 |
-
token = parsed_message.get('choices', [{}])[0].get('delta', {}).get('content', '')
|
59 |
-
except json.JSONDecodeError as e:
|
60 |
-
print(f"JSON Decode Error after cleaning: {e}")
|
61 |
-
token = ""
|
62 |
-
else:
|
63 |
-
token = message # Use the raw message as the token if parsing fails
|
64 |
else:
|
65 |
-
|
66 |
-
|
67 |
-
token = message.choices[0].delta.content
|
68 |
-
except AttributeError:
|
69 |
-
print(f"Attribute Error - message structure: {dir(message)}")
|
70 |
-
token = str(message) # Fallback to string representation
|
71 |
-
|
72 |
-
response += token
|
73 |
-
yield response
|
74 |
except Exception as e:
|
75 |
print(f"An error occurred: {e}")
|
76 |
yield f"An error occurred: {e}"
|
77 |
|
78 |
-
|
79 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
80 |
-
"""
|
81 |
demo = gr.ChatInterface(
|
82 |
respond,
|
83 |
additional_inputs=[
|
84 |
-
gr.Textbox(
|
|
|
|
|
|
|
85 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
86 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
87 |
gr.Slider(
|
@@ -93,9 +66,16 @@ demo = gr.ChatInterface(
|
|
93 |
),
|
94 |
],
|
95 |
title="Maritime Legal Compliance",
|
96 |
-
description="This chatbot uses the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
)
|
98 |
|
99 |
-
|
100 |
if __name__ == "__main__":
|
101 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
from huggingface_hub import InferenceClient
|
4 |
+
from huggingface_hub.inference._generated.types.chat_completion import ChatCompletionStreamOutput
|
|
|
5 |
|
6 |
# Use the fine-tuned maritime legal model
|
7 |
+
MODEL = "HuggingFaceH4/zephyr-7b-beta"
|
8 |
+
client = InferenceClient(MODEL)
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
|
|
31 |
temperature=temperature,
|
32 |
top_p=top_p,
|
33 |
):
|
34 |
+
if isinstance(message, ChatCompletionStreamOutput):
|
35 |
+
# Extract the content from the ChatCompletionStreamOutput object
|
36 |
+
content = message.choices[0].delta.content
|
37 |
+
if content is not None:
|
38 |
+
response += content
|
39 |
+
yield response
|
40 |
+
# Check if this is the last message in the stream
|
41 |
+
if message.choices[0].finish_reason == 'stop':
|
42 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
else:
|
44 |
+
print(f"Unexpected message type: {type(message)}")
|
45 |
+
print(f"Message content: {message}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
except Exception as e:
|
47 |
print(f"An error occurred: {e}")
|
48 |
yield f"An error occurred: {e}"
|
49 |
|
50 |
+
# Gradio interface setup
|
|
|
|
|
51 |
demo = gr.ChatInterface(
|
52 |
respond,
|
53 |
additional_inputs=[
|
54 |
+
gr.Textbox(
|
55 |
+
value="You are a maritime legal assistant with expertise strictly in Indian maritime law. Provide detailed legal advice and information based on Indian maritime legal principles and regulations.",
|
56 |
+
label="System message"
|
57 |
+
),
|
58 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
59 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
60 |
gr.Slider(
|
|
|
66 |
),
|
67 |
],
|
68 |
title="Maritime Legal Compliance",
|
69 |
+
description="This chatbot uses the Zephyr 7B Beta model to provide assistance with Indian maritime legal queries.",
|
70 |
+
theme="soft",
|
71 |
+
examples=[
|
72 |
+
["What are the key regulations governing ports in India?"],
|
73 |
+
["Explain the concept of cabotage in Indian maritime law."],
|
74 |
+
["What are the legal requirements for registering a vessel in India?"],
|
75 |
+
],
|
76 |
+
cache_examples=False,
|
77 |
)
|
78 |
|
79 |
+
# Launch the Gradio app
|
80 |
if __name__ == "__main__":
|
81 |
+
demo.launch()
|