openfree commited on
Commit
0618e1a
ยท
verified ยท
1 Parent(s): f233566

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -40
app.py CHANGED
@@ -53,23 +53,22 @@ async def end_session(session_id: str, user_email: str) -> str:
53
 
54
  def chat_streaming(message: str, user_email: str, session_id: str,
55
  web_search: bool, self_learning: bool, history: list) -> Generator:
56
- """์ŠคํŠธ๋ฆฌ๋ฐ ์ฑ„ํŒ…"""
57
  if not user_email:
58
  history = history or []
59
- history.append({"role": "assistant", "content": "โŒ ์ด๋ฉ”์ผ์„ ๋จผ์ € ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."})
60
  yield history
61
  return
62
 
63
  if not session_id:
64
  history = history or []
65
- history.append({"role": "assistant", "content": "โŒ ์„ธ์…˜์„ ๋จผ์ € ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”."})
66
  yield history
67
  return
68
 
69
  # ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
70
  history = history or []
71
- history.append({"role": "user", "content": message})
72
- history.append({"role": "assistant", "content": ""})
73
  yield history
74
 
75
  # ์ŠคํŠธ๋ฆฌ๋ฐ ์‘๋‹ต
@@ -96,12 +95,12 @@ def chat_streaming(message: str, user_email: str, session_id: str,
96
  chunk = json.loads(data)
97
  content = chunk.get("content", "")
98
  full_response += content
99
- history[-1]["content"] = full_response
100
  yield history
101
  except json.JSONDecodeError:
102
  continue
103
  except Exception as e:
104
- history[-1]["content"] = f"โŒ ์˜ค๋ฅ˜: {str(e)}"
105
  yield history
106
 
107
 
@@ -240,27 +239,6 @@ async def get_storage_info() -> str:
240
  return f"โŒ ์˜ค๋ฅ˜: {str(e)}"
241
 
242
 
243
- async def upload_file(file, session_id: str, user_email: str) -> str:
244
- """ํŒŒ์ผ ์—…๋กœ๋“œ"""
245
- if not file or not session_id or not user_email:
246
- return "โŒ ํŒŒ์ผ, ์„ธ์…˜ ID, ์ด๋ฉ”์ผ์ด ๋ชจ๋‘ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค."
247
-
248
- try:
249
- async with httpx.AsyncClient(timeout=60.0) as client:
250
- with open(file.name, "rb") as f:
251
- files = {"file": (file.name.split("/")[-1], f)}
252
- data = {"session_id": session_id, "user_email": user_email}
253
- response = await client.post(
254
- f"{API_BASE_URL}/upload/file",
255
- files=files,
256
- data=data
257
- )
258
- result = response.json()
259
- return json.dumps(result, ensure_ascii=False, indent=2)
260
- except Exception as e:
261
- return f"โŒ ์˜ค๋ฅ˜: {str(e)}"
262
-
263
-
264
  # Gradio UI ๊ตฌ์„ฑ
265
  with gr.Blocks(title="KT API ํ…Œ์ŠคํŠธ") as demo:
266
  gr.Markdown("# ๐Ÿงช KT ์‹œ๋‹ˆ์–ด AI ๋น„์„œ API ํ…Œ์ŠคํŠธ")
@@ -284,7 +262,7 @@ with gr.Blocks(title="KT API ํ…Œ์ŠคํŠธ") as demo:
284
  self_learning_check = gr.Checkbox(label="์ž๊ฐ€ ํ•™์Šต ํ™œ์„ฑํ™”", value=True)
285
 
286
  with gr.Column(scale=2):
287
- chatbot = gr.Chatbot(label="๋Œ€ํ™”", height=400, type="messages")
288
  msg_input = gr.Textbox(label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ", placeholder="๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...")
289
  send_btn = gr.Button("์ „์†ก", variant="primary")
290
 
@@ -377,17 +355,7 @@ with gr.Blocks(title="KT API ํ…Œ์ŠคํŠธ") as demo:
377
  web_search_btn.click(web_search, inputs=[web_query, web_count], outputs=[web_result])
378
  crawl_btn.click(crawl_url, inputs=[crawl_url_input], outputs=[web_result])
379
 
380
- # ํƒญ 5: ํŒŒ์ผ ์—…๋กœ๋“œ
381
- with gr.TabItem("๐Ÿ“ ํŒŒ์ผ ์—…๋กœ๋“œ"):
382
- upload_email = gr.Textbox(label="์ด๋ฉ”์ผ", placeholder="test@example.com")
383
- upload_session = gr.Textbox(label="์„ธ์…˜ ID")
384
- upload_file_input = gr.File(label="ํŒŒ์ผ ์„ ํƒ (PDF, CSV, TXT)", file_types=[".pdf", ".csv", ".txt"])
385
- upload_btn = gr.Button("์—…๋กœ๋“œ", variant="primary")
386
- upload_result = gr.Textbox(label="๊ฒฐ๊ณผ", lines=10)
387
-
388
- upload_btn.click(upload_file, inputs=[upload_file_input, upload_session, upload_email], outputs=[upload_result])
389
-
390
- # ํƒญ 6: ์‹œ์Šคํ…œ ์ •๋ณด
391
  with gr.TabItem("โš™๏ธ ์‹œ์Šคํ…œ"):
392
  storage_btn = gr.Button("์Šคํ† ๋ฆฌ์ง€ ์ •๋ณด ์กฐํšŒ", variant="primary")
393
  storage_result = gr.Textbox(label="๊ฒฐ๊ณผ", lines=15)
 
53
 
54
  def chat_streaming(message: str, user_email: str, session_id: str,
55
  web_search: bool, self_learning: bool, history: list) -> Generator:
56
+ """์ŠคํŠธ๋ฆฌ๋ฐ ์ฑ„ํŒ… - ํŠœํ”Œ ํ˜•์‹ ์‚ฌ์šฉ"""
57
  if not user_email:
58
  history = history or []
59
+ history.append(["", "โŒ ์ด๋ฉ”์ผ์„ ๋จผ์ € ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."])
60
  yield history
61
  return
62
 
63
  if not session_id:
64
  history = history or []
65
+ history.append(["", "โŒ ์„ธ์…˜์„ ๋จผ์ € ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”."])
66
  yield history
67
  return
68
 
69
  # ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
70
  history = history or []
71
+ history.append([message, ""])
 
72
  yield history
73
 
74
  # ์ŠคํŠธ๋ฆฌ๋ฐ ์‘๋‹ต
 
95
  chunk = json.loads(data)
96
  content = chunk.get("content", "")
97
  full_response += content
98
+ history[-1][1] = full_response
99
  yield history
100
  except json.JSONDecodeError:
101
  continue
102
  except Exception as e:
103
+ history[-1][1] = f"โŒ ์˜ค๋ฅ˜: {str(e)}"
104
  yield history
105
 
106
 
 
239
  return f"โŒ ์˜ค๋ฅ˜: {str(e)}"
240
 
241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  # Gradio UI ๊ตฌ์„ฑ
243
  with gr.Blocks(title="KT API ํ…Œ์ŠคํŠธ") as demo:
244
  gr.Markdown("# ๐Ÿงช KT ์‹œ๋‹ˆ์–ด AI ๋น„์„œ API ํ…Œ์ŠคํŠธ")
 
262
  self_learning_check = gr.Checkbox(label="์ž๊ฐ€ ํ•™์Šต ํ™œ์„ฑํ™”", value=True)
263
 
264
  with gr.Column(scale=2):
265
+ chatbot = gr.Chatbot(label="๋Œ€ํ™”", height=400)
266
  msg_input = gr.Textbox(label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ", placeholder="๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...")
267
  send_btn = gr.Button("์ „์†ก", variant="primary")
268
 
 
355
  web_search_btn.click(web_search, inputs=[web_query, web_count], outputs=[web_result])
356
  crawl_btn.click(crawl_url, inputs=[crawl_url_input], outputs=[web_result])
357
 
358
+ # ํƒญ 5: ์‹œ์Šคํ…œ ์ •๋ณด
 
 
 
 
 
 
 
 
 
 
359
  with gr.TabItem("โš™๏ธ ์‹œ์Šคํ…œ"):
360
  storage_btn = gr.Button("์Šคํ† ๋ฆฌ์ง€ ์ •๋ณด ์กฐํšŒ", variant="primary")
361
  storage_result = gr.Textbox(label="๊ฒฐ๊ณผ", lines=15)