aiqtech commited on
Commit
c0a7a64
Β·
verified Β·
1 Parent(s): 00ea9bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -3,6 +3,7 @@ from huggingface_hub import InferenceClient, HfApi
3
  import os
4
  import requests
5
  import pandas as pd
 
6
 
7
  # Hugging Face 토큰 확인
8
  hf_token = os.getenv("HF_TOKEN")
@@ -35,6 +36,8 @@ def get_prompt(act):
35
  matching_prompt = prompts_df[prompts_df['act'] == act]['prompt'].values
36
  return matching_prompt[0] if len(matching_prompt) > 0 else None
37
 
 
 
38
  def respond(
39
  message,
40
  history: list[tuple[str, str]],
@@ -68,32 +71,37 @@ def respond(
68
 
69
  def query(payload):
70
  response = requests.post(API_URL, headers=headers, json=payload)
71
- return response.json()
72
 
73
  try:
74
- output = query({
75
  "inputs": messages,
76
  "parameters": {
77
  "max_new_tokens": max_tokens,
78
  "temperature": temperature,
79
  "top_p": top_p,
80
- "stream": True
81
  },
82
- })
83
-
84
- if isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
85
- response = output[0]["generated_text"]
86
- yield response
87
- else:
88
- print("Unexpected API response:", output)
89
- yield "μ£„μ†‘ν•©λ‹ˆλ‹€. μ˜ˆμƒμΉ˜ λͺ»ν•œ 응닡 ν˜•μ‹μž…λ‹ˆλ‹€."
 
 
 
 
90
 
91
  except Exception as e:
92
  print(f"Error during API request: {e}")
93
- yield f"μ£„μ†‘ν•©λ‹ˆλ‹€. 응닡 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
 
 
94
 
95
- if not response:
96
- yield "μ£„μ†‘ν•©λ‹ˆλ‹€. 응닡을 μƒμ„±ν•˜μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€."
97
 
98
  demo = gr.ChatInterface(
99
  respond,
 
3
  import os
4
  import requests
5
  import pandas as pd
6
+ import json
7
 
8
  # Hugging Face 토큰 확인
9
  hf_token = os.getenv("HF_TOKEN")
 
36
  matching_prompt = prompts_df[prompts_df['act'] == act]['prompt'].values
37
  return matching_prompt[0] if len(matching_prompt) > 0 else None
38
 
39
+
40
+
41
  def respond(
42
  message,
43
  history: list[tuple[str, str]],
 
71
 
72
  def query(payload):
73
  response = requests.post(API_URL, headers=headers, json=payload)
74
+ return response.text # μ›μ‹œ 응닡 ν…μŠ€νŠΈ λ°˜ν™˜
75
 
76
  try:
77
+ payload = {
78
  "inputs": messages,
79
  "parameters": {
80
  "max_new_tokens": max_tokens,
81
  "temperature": temperature,
82
  "top_p": top_p,
83
+ "stream": False # μŠ€νŠΈλ¦¬λ°μ„ λΉ„ν™œμ„±ν™”
84
  },
85
+ }
86
+ raw_response = query(payload)
87
+ print("Raw API response:", raw_response) # 디버깅을 μœ„ν•΄ μ›μ‹œ 응닡 좜λ ₯
88
+
89
+ try:
90
+ output = json.loads(raw_response)
91
+ if isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
92
+ response = output[0]["generated_text"]
93
+ else:
94
+ response = f"μ˜ˆμƒμΉ˜ λͺ»ν•œ 응닡 ν˜•μ‹μž…λ‹ˆλ‹€: {output}"
95
+ except json.JSONDecodeError:
96
+ response = f"JSON λ””μ½”λ”© 였λ₯˜. μ›μ‹œ 응닡: {raw_response}"
97
 
98
  except Exception as e:
99
  print(f"Error during API request: {e}")
100
+ response = f"μ£„μ†‘ν•©λ‹ˆλ‹€. 응닡 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
101
+
102
+ yield response
103
 
104
+ # λ‚˜λ¨Έμ§€ μ½”λ“œλŠ” κ·ΈλŒ€λ‘œ μœ μ§€
 
105
 
106
  demo = gr.ChatInterface(
107
  respond,