Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,69 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
| 7 |
history: list[dict[str, str]],
|
| 8 |
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
"""
|
| 15 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
|
| 21 |
-
messages.extend(history)
|
| 22 |
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
-
response = ""
|
| 26 |
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
"""
|
|
@@ -47,16 +73,16 @@ chatbot = gr.ChatInterface(
|
|
| 47 |
respond,
|
| 48 |
type="messages",
|
| 49 |
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
),
|
| 60 |
],
|
| 61 |
)
|
| 62 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
CHAT_URL = os.getenv("CHAT_URL")
|
| 6 |
+
TOKEN = os.getenv("TOKEN")
|
| 7 |
+
PROJECT_ID = os.getenv("PROJECT_ID")
|
| 8 |
|
| 9 |
def respond(
|
| 10 |
message,
|
| 11 |
history: list[dict[str, str]],
|
| 12 |
system_message,
|
| 13 |
+
# max_tokens,
|
| 14 |
+
# temperature,
|
| 15 |
+
# top_p,
|
| 16 |
+
# hf_token: gr.OAuthToken,
|
| 17 |
):
|
| 18 |
"""
|
| 19 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 20 |
"""
|
| 21 |
+
# client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 22 |
|
| 23 |
+
# messages = [{"role": "system", "content": system_message}]
|
| 24 |
|
| 25 |
+
# messages.extend(history)
|
| 26 |
|
| 27 |
+
# messages.append({"role": "user", "content": message})
|
| 28 |
|
| 29 |
+
# response = ""
|
| 30 |
|
| 31 |
+
# for message in client.chat_completion(
|
| 32 |
+
# messages,
|
| 33 |
+
# max_tokens=max_tokens,
|
| 34 |
+
# stream=True,
|
| 35 |
+
# temperature=temperature,
|
| 36 |
+
# top_p=top_p,
|
| 37 |
+
# ):
|
| 38 |
+
# choices = message.choices
|
| 39 |
+
# token = ""
|
| 40 |
+
# if len(choices) and choices[0].delta.content:
|
| 41 |
+
# token = choices[0].delta.content
|
| 42 |
|
| 43 |
+
# response += token
|
| 44 |
+
# yield response
|
| 45 |
+
|
| 46 |
+
req = requests.post(
|
| 47 |
+
"project_id": PROJECT_ID,
|
| 48 |
+
"session_id":system_message,
|
| 49 |
+
"user_input":message,
|
| 50 |
+
"update_variables": { // update_variables wajib kalau tidak pakai #ai
|
| 51 |
+
"alfamidi":"",
|
| 52 |
+
"alfagift":"",
|
| 53 |
+
"karir":"",
|
| 54 |
+
"layanan":"",
|
| 55 |
+
"program":"",
|
| 56 |
+
"voucher":"",
|
| 57 |
+
"results":"",
|
| 58 |
+
"identitas":"",
|
| 59 |
+
"kerjasama":""
|
| 60 |
+
},
|
| 61 |
+
"output_variables": ["results"]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
out = req.json()["data"]["results"]
|
| 65 |
+
|
| 66 |
+
return out
|
| 67 |
|
| 68 |
|
| 69 |
"""
|
|
|
|
| 73 |
respond,
|
| 74 |
type="messages",
|
| 75 |
additional_inputs=[
|
| 76 |
+
gr.Textbox(value="TEST_123", label="session_id"),
|
| 77 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 78 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 79 |
+
# gr.Slider(
|
| 80 |
+
# minimum=0.1,
|
| 81 |
+
# maximum=1.0,
|
| 82 |
+
# value=0.95,
|
| 83 |
+
# step=0.05,
|
| 84 |
+
# label="Top-p (nucleus sampling)",
|
| 85 |
+
# ),
|
| 86 |
],
|
| 87 |
)
|
| 88 |
|