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