aiqtech commited on
Commit
b6f8016
ยท
verified ยท
1 Parent(s): e491663

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -13
app.py CHANGED
@@ -1,11 +1,25 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
  import os
4
  import requests
5
  import pandas as pd
6
 
7
- client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=os.getenv("HF_TOKEN"))
 
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # ํ˜„์žฌ ์Šคํฌ๋ฆฝํŠธ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€ ๊ฒฝ๋กœ ์„ค์ •
11
  current_dir = os.path.dirname(os.path.abspath(__file__))
@@ -48,17 +62,28 @@ def respond(
48
 
49
  response = ""
50
 
51
- for message in client.chat_completion(
52
- messages,
53
- max_tokens=max_tokens,
54
- stream=True,
55
- temperature=temperature,
56
- top_p=top_p,
57
- ):
58
- token = message.choices[0].delta.content
59
- if token is not None:
60
- response += token.strip("<|END_OF_TURN_TOKEN|>")
61
- yield response
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  demo = gr.ChatInterface(
64
  respond,
 
1
  import gradio as gr
2
+ 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")
9
+ if not hf_token:
10
+ raise ValueError("HF_TOKEN ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
11
 
12
+ # ๋ชจ๋ธ ์ •๋ณด ํ™•์ธ
13
+ api = HfApi(token=hf_token)
14
+ try:
15
+ model_info = api.model_info("meta-llama/Meta-Llama-3-70B-Instruct")
16
+ print(f"๋ชจ๋ธ ์ •๋ณด: {model_info}")
17
+ except Exception as e:
18
+ print(f"๋ชจ๋ธ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค: {e}")
19
+ # ๋Œ€์ฒด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์„ธ์š”.
20
+
21
+ # InferenceClient ์ดˆ๊ธฐํ™”
22
+ client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=hf_token)
23
 
24
  # ํ˜„์žฌ ์Šคํฌ๋ฆฝํŠธ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€ ๊ฒฝ๋กœ ์„ค์ •
25
  current_dir = os.path.dirname(os.path.abspath(__file__))
 
62
 
63
  response = ""
64
 
65
+ try:
66
+ for message in client.chat_completion(
67
+ messages,
68
+ max_tokens=max_tokens,
69
+ stream=True,
70
+ temperature=temperature,
71
+ top_p=top_p,
72
+ ):
73
+ if message is not None and hasattr(message, 'choices') and len(message.choices) > 0:
74
+ delta = message.choices[0].delta
75
+ if delta is not None and hasattr(delta, 'content') and delta.content is not None:
76
+ token = delta.content.strip("<|END_OF_TURN_TOKEN|>")
77
+ response += token
78
+ yield response
79
+ else:
80
+ print("Received unexpected message format:", message)
81
+ except Exception as e:
82
+ print(f"Error during chat completion: {e}")
83
+ yield f"์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์‘๋‹ต ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
84
+
85
+ if not response:
86
+ yield "์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์‘๋‹ต์„ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค."
87
 
88
  demo = gr.ChatInterface(
89
  respond,