Spaces:
Sleeping
Sleeping
devin_lee
commited on
Commit
·
8e477f3
1
Parent(s):
980a07f
commit application files.
Browse files- app.py +66 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import outlines
|
| 3 |
+
import os, json
|
| 4 |
+
from outlines.models import openai
|
| 5 |
+
from outlines.generate import choice
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
model_list = ["google/gemma-2b-it",
|
| 9 |
+
"Qwen/Qwen2.5-7B-Instruct-Turbo"]
|
| 10 |
+
model = openai(
|
| 11 |
+
model_list[0],
|
| 12 |
+
api_key=os.getenv("TOGETHER_AI_KEY"),
|
| 13 |
+
base_url="https://api.together.xyz/v1"
|
| 14 |
+
)
|
| 15 |
+
classifier = choice(model, ["Reservation", "Others"])
|
| 16 |
+
|
| 17 |
+
def create_prompt_message(message: str) -> str:
|
| 18 |
+
return f"""
|
| 19 |
+
You are an agent that classifies restaurant-related user messages.
|
| 20 |
+
|
| 21 |
+
Your job is to classify the following message into one of two categories: "Reservation" or "Others".
|
| 22 |
+
|
| 23 |
+
You MUST respond EXACTLY with one of these two JSON objects:
|
| 24 |
+
|
| 25 |
+
{{"result": "Reservation"}} or {{"result": "Others"}}
|
| 26 |
+
|
| 27 |
+
Do not add any explanation or other content.
|
| 28 |
+
|
| 29 |
+
Message: {message}
|
| 30 |
+
""".strip()
|
| 31 |
+
|
| 32 |
+
def create_prompt(message:str) -> str:
|
| 33 |
+
prompt = create_prompt_message(message)
|
| 34 |
+
try:
|
| 35 |
+
result = classifier(prompt)
|
| 36 |
+
return result
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"[Error] Failed to classify message: {e}")
|
| 39 |
+
return "Error"
|
| 40 |
+
|
| 41 |
+
def inference(message:str)->str:
|
| 42 |
+
try:
|
| 43 |
+
result = create_prompt(message)
|
| 44 |
+
if result == 'Reservation':
|
| 45 |
+
result = "要訂位!"
|
| 46 |
+
elif result == 'Others':
|
| 47 |
+
result = "非訂位!"
|
| 48 |
+
else:
|
| 49 |
+
result = "不明意圖。"
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"[Error!]: {e}")
|
| 52 |
+
return "Error!"
|
| 53 |
+
|
| 54 |
+
return result
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
iface = gr.Interface(
|
| 58 |
+
fn=inference,
|
| 59 |
+
inputs=gr.Textbox(lines=2, placeholder="請輸入顧客訊息...", label="輸入訊息"),
|
| 60 |
+
outputs=gr.Textbox(label="分類結果"),
|
| 61 |
+
title="訂位意圖分類器",
|
| 62 |
+
description="判斷輸入訊息是否與訂位有關"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
iface.launch()
|
| 66 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=5.0.0
|
| 2 |
+
outlines
|
| 3 |
+
openai
|