seawolf2357 commited on
Commit
0e0988e
·
verified ·
1 Parent(s): 3c19628

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -1,9 +1,8 @@
1
  import gradio as gr
2
  import requests
3
  import os
4
- import json # json 모듈 임포트 추가
5
 
6
- # 환경 변수에서 Hugging Face API 토큰을 가져옵니다.
7
  ACCESS_TOKEN = os.getenv("HF_TOKEN")
8
 
9
  def respond(message, max_tokens=512, temperature=0.7, top_p=0.95):
@@ -15,19 +14,25 @@ def respond(message, max_tokens=512, temperature=0.7, top_p=0.95):
15
  "top_p": top_p
16
  }
17
 
18
- # 외부 API 요청을 보냄
19
  response = requests.post("http://hugpu.ai:7877/api/generate", json=data)
20
-
21
- # API 응답을 로그로 출력
22
- print("API Response:", response.text)
23
-
24
  try:
25
- generated_text = response.json().get('generated_text', '') # API 응답에서 텍스트 추출
 
 
 
 
 
 
 
 
 
 
26
  except json.JSONDecodeError as e:
27
  print("Failed to decode JSON from response:", e)
28
  generated_text = "An error occurred while processing your request."
29
-
30
- return generated_text # 생성된 텍스트 반환
31
 
32
  demo = gr.Interface(
33
  fn=respond,
 
1
  import gradio as gr
2
  import requests
3
  import os
4
+ import json
5
 
 
6
  ACCESS_TOKEN = os.getenv("HF_TOKEN")
7
 
8
  def respond(message, max_tokens=512, temperature=0.7, top_p=0.95):
 
14
  "top_p": top_p
15
  }
16
 
17
+ # API 요청
18
  response = requests.post("http://hugpu.ai:7877/api/generate", json=data)
 
 
 
 
19
  try:
20
+ # 개별 JSON 객체들을 분리하여 처리
21
+ responses = []
22
+ for obj in response.text.strip().split('\n'):
23
+ # 각 줄을 JSON으로 파싱
24
+ result = json.loads(obj)
25
+ if result.get("done", False):
26
+ break
27
+ responses.append(result.get('response', ''))
28
+
29
+ # 결과 텍스트를 조합
30
+ generated_text = ''.join(responses)
31
  except json.JSONDecodeError as e:
32
  print("Failed to decode JSON from response:", e)
33
  generated_text = "An error occurred while processing your request."
34
+
35
+ return generated_text
36
 
37
  demo = gr.Interface(
38
  fn=respond,