asthaa30 commited on
Commit
22727a0
·
verified ·
1 Parent(s): ba75572

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -48
app.py CHANGED
@@ -1,70 +1,65 @@
1
  import gradio as gr
2
  import json
3
- import os
4
  from huggingface_hub import InferenceClient
5
- from groq import Groq
6
- from groq.types.chat.chat_completion_tool_param import ChatCompletionToolParam
7
 
8
- # Use the fine-tuned maritime legal model
9
- MODEL = "nomiChroma3.1"
10
- ##client = Groq(api_key=os.environ["GROQ_API_KEY"])
11
 
12
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
13
 
14
-
15
- def respond(
16
- message,
17
- history: list[tuple[str, str]],
18
- system_message,
19
- max_tokens,
20
- temperature,
21
- top_p,
22
- ):
23
  messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
24
 
25
- for val in history:
26
- if val[0]:
27
- messages.append({"role": "user", "content": val[0]})
28
- if val[1]:
29
- messages.append({"role": "assistant", "content": val[1]})
30
-
31
  messages.append({"role": "user", "content": message})
32
 
33
  response = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- for message in client.chat_completion(
36
- messages,
37
- max_tokens=max_tokens,
38
- stream=True,
39
- temperature=temperature,
40
- top_p=top_p,
41
- ):
42
- token = message.choices[0].delta.content
43
-
44
- response += token
45
- yield response
46
 
47
- """
48
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
49
- """
50
  demo = gr.ChatInterface(
51
  respond,
52
  additional_inputs=[
53
- gr.Textbox(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.", label="System message"),
54
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
55
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
56
- gr.Slider(
57
- minimum=0.1,
58
- maximum=1.0,
59
- value=0.95,
60
- step=0.05,
61
- label="Top-p (nucleus sampling)",
62
  ),
 
 
 
63
  ],
64
  title="Maritime Legal Compliance",
65
- description="This chatbot uses the fine tune Llama 3.1 which has the capabilities of responding and helping in legal advices regarding maritime",
66
  )
67
 
68
-
69
  if __name__ == "__main__":
70
- demo.launch()
 
1
  import gradio as gr
2
  import json
 
3
  from huggingface_hub import InferenceClient
 
 
4
 
5
+ # Hugging Face model for maritime legal advice
6
+ MODEL = "HuggingFaceH4/zephyr-7b-beta"
 
7
 
8
+ # Initialize the client
9
+ client = InferenceClient(MODEL)
10
 
11
+ def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
12
+ # Build the messages list for the model
 
 
 
 
 
 
 
13
  messages = [{"role": "system", "content": system_message}]
14
+
15
+ # Add conversation history
16
+ for user_input, assistant_response in history:
17
+ if user_input:
18
+ messages.append({"role": "user", "content": user_input})
19
+ if assistant_response:
20
+ messages.append({"role": "assistant", "content": assistant_response})
21
 
22
+ # Add the latest user input
 
 
 
 
 
23
  messages.append({"role": "user", "content": message})
24
 
25
  response = ""
26
+
27
+ try:
28
+ # Streaming the chat completion response
29
+ for message in client.chat_completion(
30
+ messages,
31
+ max_tokens=max_tokens,
32
+ stream=True,
33
+ temperature=temperature,
34
+ top_p=top_p,
35
+ ):
36
+ try:
37
+ # Safely parse each streamed message item
38
+ payload = message.choices[0].delta.content
39
+ response += payload
40
+ yield response
41
+ except json.JSONDecodeError as e:
42
+ # Handle JSON parsing error
43
+ yield f"Error decoding JSON: {str(e)}"
44
 
45
+ except Exception as e:
46
+ yield f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
47
 
48
+ # Gradio interface
 
 
49
  demo = gr.ChatInterface(
50
  respond,
51
  additional_inputs=[
52
+ gr.Textbox(
53
+ 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.",
54
+ label="System message"
 
 
 
 
 
 
55
  ),
56
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
57
+ gr.Slider(minimum=0.1, maximum 4.0, value=0.7, step=0.1, label="Temperature"),
58
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
59
  ],
60
  title="Maritime Legal Compliance",
61
+ description="This chatbot uses a fine-tuned model for providing legal advice on Indian maritime law.",
62
  )
63
 
 
64
  if __name__ == "__main__":
65
+ demo.launch()