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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -6,7 +6,8 @@ import pandas as pd
6
 
7
  # Hugging Face 토큰 확인
8
  hf_token = os.getenv("HF_TOKEN")
9
- if not hf_token:
 
10
  raise ValueError("HF_TOKEN 환경 변수가 설정되지 않았습니다.")
11
 
12
  # 모델 정보 확인
@@ -62,29 +63,33 @@ def respond(
62
 
63
  messages.append({"role": "user", "content": message})
64
 
65
- response = ""
 
 
 
 
 
66
 
67
  try:
68
- for message in client.chat_completion(
69
- messages,
70
- max_tokens=max_tokens,
71
- stream=True,
72
- temperature=temperature,
73
- top_p=top_p,
74
- ):
75
- if message is not None:
76
- if hasattr(message, 'choices') and len(message.choices) > 0:
77
- delta = message.choices[0].delta
78
- if hasattr(delta, 'content') and delta.content is not None:
79
- token = delta.content.strip("<|END_OF_TURN_TOKEN|>")
80
- response += token
81
- yield response
82
- else:
83
- print("Received message without choices:", message)
84
- else:
85
- print("Received None message")
86
  except Exception as e:
87
- print(f"Error during chat completion: {e}")
88
  yield f"죄송합니다. 응답 생성 중 오류가 발생했습니다: {str(e)}"
89
 
90
  if not response:
 
6
 
7
  # Hugging Face 토큰 확인
8
  hf_token = os.getenv("HF_TOKEN")
9
+
10
+ if not os.getenv("HF_TOKEN"):
11
  raise ValueError("HF_TOKEN 환경 변수가 설정되지 않았습니다.")
12
 
13
  # 모델 정보 확인
 
63
 
64
  messages.append({"role": "user", "content": message})
65
 
66
+ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct"
67
+ headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
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: