Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
@@ -7,8 +7,7 @@ from fastapi import FastAPI, Request
|
|
7 |
from fastapi.responses import StreamingResponse
|
8 |
import sentencepiece as spm
|
9 |
import re
|
10 |
-
app = FastAPI()
|
11 |
-
|
12 |
|
13 |
from fastapi.middleware.cors import CORSMiddleware
|
14 |
|
@@ -285,6 +284,11 @@ def summarize_from_wikipedia(query, top_n=3):
|
|
285 |
raw_summary = get_wikipedia_summary(query)
|
286 |
return textrank_summarize(raw_summary, top_n=top_n)
|
287 |
|
|
|
|
|
|
|
|
|
|
|
288 |
# 의도 분류기
|
289 |
def simple_intent_classifier(text):
|
290 |
text = text.lower()
|
@@ -309,8 +313,10 @@ def parse_math_question(text):
|
|
309 |
except:
|
310 |
return "계산할 수 없는 수식이에요. 다시 한번 확인해 주세요!"
|
311 |
|
312 |
-
#
|
313 |
def respond(input_text):
|
|
|
|
|
314 |
intent = simple_intent_classifier(input_text)
|
315 |
|
316 |
if "이름" in input_text:
|
@@ -320,22 +326,38 @@ def respond(input_text):
|
|
320 |
return "저는 Ector.V라고 해요."
|
321 |
|
322 |
if intent == "수학질문":
|
323 |
-
|
|
|
|
|
|
|
324 |
|
325 |
if intent == "인사":
|
326 |
-
|
|
|
|
|
|
|
327 |
|
328 |
if intent == "정보질문":
|
329 |
keyword = re.sub(r"(에 대해|에 대한|에 대해서)?\s*(설명해줘|알려줘|뭐야|개념|정의|정보)?", "", input_text).strip()
|
330 |
if not keyword:
|
331 |
return "어떤 주제에 대해 궁금한가요?"
|
332 |
summary = summarize_from_wikipedia(keyword)
|
333 |
-
|
|
|
|
|
|
|
334 |
|
335 |
-
# 일상 대화:
|
336 |
-
|
|
|
|
|
|
|
337 |
if not is_valid_response(response) or mismatch_tone(input_text, response):
|
338 |
-
response = generate_text_greedy_strong(model,
|
|
|
|
|
|
|
|
|
339 |
return response
|
340 |
|
341 |
@app.get("/generate", response_class=PlainTextResponse)
|
|
|
7 |
from fastapi.responses import StreamingResponse
|
8 |
import sentencepiece as spm
|
9 |
import re
|
10 |
+
app = FastAPI()
|
|
|
11 |
|
12 |
from fastapi.middleware.cors import CORSMiddleware
|
13 |
|
|
|
284 |
raw_summary = get_wikipedia_summary(query)
|
285 |
return textrank_summarize(raw_summary, top_n=top_n)
|
286 |
|
287 |
+
def build_contexted_prompt(history: List[str], user_input: str):
|
288 |
+
recent = ' '.join(history[-3:])
|
289 |
+
summary = textrank_summarize(recent, top_n=2)
|
290 |
+
return f"이전 대화 요약: {summary}\n사용자: {user_input}"
|
291 |
+
|
292 |
# 의도 분류기
|
293 |
def simple_intent_classifier(text):
|
294 |
text = text.lower()
|
|
|
313 |
except:
|
314 |
return "계산할 수 없는 수식이에요. 다시 한번 확인해 주세요!"
|
315 |
|
316 |
+
# respond 함수 수정
|
317 |
def respond(input_text):
|
318 |
+
global dialogue_history
|
319 |
+
|
320 |
intent = simple_intent_classifier(input_text)
|
321 |
|
322 |
if "이름" in input_text:
|
|
|
326 |
return "저는 Ector.V라고 해요."
|
327 |
|
328 |
if intent == "수학질문":
|
329 |
+
dialogue_history.append(f"사용자: {input_text}")
|
330 |
+
response = parse_math_question(input_text)
|
331 |
+
dialogue_history.append(f"Ector: {response}")
|
332 |
+
return response
|
333 |
|
334 |
if intent == "인사":
|
335 |
+
response = "반가워요! 무엇을 도와드릴까요?"
|
336 |
+
dialogue_history.append(f"사용자: {input_text}")
|
337 |
+
dialogue_history.append(f"Ector: {response}")
|
338 |
+
return response
|
339 |
|
340 |
if intent == "정보질문":
|
341 |
keyword = re.sub(r"(에 대해|에 대한|에 대해서)?\s*(설명해줘|알려줘|뭐야|개념|정의|정보)?", "", input_text).strip()
|
342 |
if not keyword:
|
343 |
return "어떤 주제에 대해 궁금한가요?"
|
344 |
summary = summarize_from_wikipedia(keyword)
|
345 |
+
response = f"{summary}\n다른 궁금한 점 있으신가요?"
|
346 |
+
dialogue_history.append(f"사용자: {input_text}")
|
347 |
+
dialogue_history.append(f"Ector: {response}")
|
348 |
+
return response
|
349 |
|
350 |
+
# 일상 대화: 요약 기반 컨텍스트 생성
|
351 |
+
contexted_prompt = build_contexted_prompt(dialogue_history, input_text)
|
352 |
+
response = generate_text_greedy_strong(model, contexted_prompt)
|
353 |
+
|
354 |
+
# fallback
|
355 |
if not is_valid_response(response) or mismatch_tone(input_text, response):
|
356 |
+
response = generate_text_greedy_strong(model, contexted_prompt)
|
357 |
+
|
358 |
+
# 히스토리 추가
|
359 |
+
dialogue_history.append(f"사용자: {input_text}")
|
360 |
+
dialogue_history.append(f"Ector: {response}")
|
361 |
return response
|
362 |
|
363 |
@app.get("/generate", response_class=PlainTextResponse)
|