asthaa30 commited on
Commit
0374538
·
verified ·
1 Parent(s): a30a4a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -18
app.py CHANGED
@@ -1,27 +1,86 @@
1
- # test_groq_api.py
2
 
3
- import os
 
 
 
4
  from groq import Groq
 
 
5
 
6
  # Initialize Groq API client
7
- client = Groq(api_key=os.environ["GROQ_API_KEY"])
 
 
 
8
 
9
- # Model identifier for Groq API
10
- model_name = "asthaa30/l3.1"
 
 
 
 
 
11
 
12
- messages = [
13
- {"role": "system", "content": "You are a friendly Chatbot."},
14
- {"role": "user", "content": "Hello!"},
15
- ]
16
 
 
17
  try:
18
- response = client.chat.completions.create(
19
- model=model_name,
20
- messages=messages,
21
- max_tokens=512,
22
- temperature=0.7,
23
- top_p=0.95,
24
- )
25
- print(response)
26
  except Exception as e:
27
- print(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
+ # app.py
3
+
4
+ import gradio as gr
5
+ from transformers import AutoTokenizer
6
  from groq import Groq
7
+ import os
8
+ from huggingface_hub import login
9
 
10
  # Initialize Groq API client
11
+ try:
12
+ client = Groq(api_key=os.environ["GROQ_API_KEY"])
13
+ except KeyError:
14
+ raise ValueError("GROQ_API_KEY environment variable not set.")
15
 
16
+ # Load the Hugging Face token from the environment variable
17
+ hf_token = os.getenv('HF_TOKEN')
18
+ if hf_token is None:
19
+ raise ValueError("Hugging Face token not found. Please set it as an environment variable.")
20
+
21
+ # Authentication token for Hugging Face
22
+ login(token=hf_token)
23
 
24
+ # Model identifier for Groq API (you can replace it with your HF model if needed)
25
+ model_name = "asthaa30/l3.1"
 
 
26
 
27
+ # Load tokenizer (model will be accessed via Groq API)
28
  try:
29
+ tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
 
 
 
 
 
 
 
30
  except Exception as e:
31
+ raise ValueError(f"Failed to load tokenizer: {e}")
32
+
33
+ def respond(
34
+ message: str,
35
+ history: list[tuple[str, str]],
36
+ system_message: str,
37
+ max_tokens: int,
38
+ temperature: float,
39
+ top_p: float,
40
+ ) -> str:
41
+ messages = [{"role": "system", "content": system_message}]
42
+
43
+ for user_msg, assistant_msg in history:
44
+ if user_msg:
45
+ messages.append({"role": "user", "content": user_msg})
46
+ if assistant_msg:
47
+ messages.append({"role": "assistant", "content": assistant_msg})
48
+
49
+ messages.append({"role": "user", "content": message})
50
+
51
+ # Use Groq API to get the model's response
52
+ try:
53
+ response = client.chat.completions.create(
54
+ model=model_name,
55
+ messages=messages,
56
+ max_tokens=max_tokens,
57
+ temperature=temperature,
58
+ top_p=top_p,
59
+ )
60
+ assistant_message = response.choices[0].message['content']
61
+ except Exception as e:
62
+ print(f"An error occurred while getting model response: {str(e)}")
63
+ assistant_message = "An error occurred. Please try again later."
64
+
65
+ return assistant_message
66
+
67
+ demo = gr.ChatInterface(
68
+ respond,
69
+ additional_inputs=[
70
+ gr.Textbox(value="You are an experienced maritime legal assistant.", label="System message"),
71
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
72
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
73
+ gr.Slider(
74
+ minimum=0.1,
75
+ maximum=1.0,
76
+ value=0.95,
77
+ step=0.05,
78
+ label="Top-p (nucleus sampling)",
79
+ ),
80
+ ],
81
+ title="Maritime Legal Assistant",
82
+ description="This chatbot provides legal assistance related to maritime laws using a fine-tuned model hosted on Hugging Face and integrated with Groq API.",
83
+ )
84
+
85
+ if __name__ == "__main__":
86
+ demo.launch()