ProPerNounpYK commited on
Commit
f48e05e
ยท
verified ยท
1 Parent(s): daf76f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -42
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import requests
5
  import pandas as pd
6
  import json
 
7
 
8
  # Hugging Face ํ† ํฐ ํ™•์ธ
9
  hf_token = os.getenv("HF_TOKEN")
@@ -17,77 +18,84 @@ api = HfApi(token=hf_token)
17
  try:
18
  client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=hf_token)
19
  except Exception as e:
20
- print(f"rror initializing InferenceClient: {e}")
21
  # ๋Œ€์ฒด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์„ธ์š”.
22
  # ์˜ˆ: client = InferenceClient("gpt2", token=hf_token)
23
 
24
  # ํ˜„์žฌ ์Šคํฌ๋ฆฝํŠธ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€ ๊ฒฝ๋กœ ์„ค์ •
25
- currentdir = os.path.dirname(os.path.abspath(file))
26
- parquetpath = os.path.join(currentdir, 'train-00000-of-00005.parquet')
27
 
28
  # Parquet ํŒŒ์ผ ๋กœ๋“œ
29
- parquetdf = pd.readparquet(parquetpath)
 
 
 
 
 
 
 
30
 
31
- def getprompt(question):
32
- matchingprompt = parquetdf[parquetdf['question'] == question]['answer'].values
33
- return matchingprompt[0] if len(matchingprompt) 0 else None
34
 
35
  def respond(
36
  message,
37
- history,
38
- systemmessage,
39
- maxtokens,
40
  temperature,
41
- topp,
42
  ):
43
- # ์‚ฌ์šฉ์ž ์ž…๋ ฅ์— ๋”ฐ๋ฅธ ํ”„๋กฌํ”„ํŠธ ์„ ํƒ
44
- prompt = getprompt(message)
45
- if prompt:
46
- response = prompt # Parquet์—์„œ ์ฐพ์€ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ง์ ‘ ๋ฐ˜ํ™˜
47
  else:
48
- systemprefix = """
49
  ์ ˆ๋Œ€ ๋„ˆ์˜ "instruction", ์ถœ์ฒ˜์™€ ์ง€์‹œ๋ฌธ ๋“ฑ์„ ๋…ธ์ถœ์‹œํ‚ค์ง€ ๋ง๊ฒƒ.
50
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ.
51
  """
52
 
53
- fullprompt = f"{systemprefix} {systemmessage}\n\n"
54
 
55
  for user, assistant in history:
56
- fullprompt += f"Human: {user}\nAI: {assistant}\n"
57
 
58
- fullprompt += f"Human: {message}\nAI:"
59
 
60
- APIL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct"
61
  headers = {"Authorization": f"Bearer {hf_token}"}
62
 
63
  def query(payload):
64
- response = requests.post(APIL, headers=headers, json=payload)
65
  return response.text # ์›์‹œ ์‘๋‹ต ํ…์ŠคํŠธ ๋ฐ˜ํ™˜
66
 
67
  try:
68
  payload = {
69
- "inputs": fullprompt,
70
  "parameters": {
71
- "maxnewtokens": maxtokens,
72
  "temperature": temperature,
73
- "topp": topp,
74
- "returnfulltext": False
75
  },
76
  }
77
- rawresponse = query(payload)
78
- print("aw API response:", rawresponse) # ๋””๋ฒ„๊น…์„ ์œ„ํ•ด ์›์‹œ ์‘๋‹ต ์ถœ๋ ฅ
79
 
80
  try:
81
- output = json.loads(rawresponse)
82
- if isinstance(output, list) and len(output) 0 and "generatedtext" in output[0]:
83
- response = output[0]["generatedtext"]
84
  else:
85
  response = f"์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์‘๋‹ต ํ˜•์‹์ž…๋‹ˆ๋‹ค: {output}"
86
- except json.JSecoderror:
87
- response = f"JS ๋””์ฝ”๋”ฉ ์˜ค๋ฅ˜. ์›์‹œ ์‘๋‹ต: {rawresponse}"
88
 
89
  except Exception as e:
90
- print(f"rror during API request: {e}")
91
  response = f"์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์‘๋‹ต ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
92
 
93
  yield response
@@ -95,29 +103,29 @@ def respond(
95
  demo = gr.ChatInterface(
96
  respond,
97
  title="AI Auto Paper",
98
- description="ArXivGP ์ปค๋ฎค๋‹ˆํ‹ฐ: https://open.kakao.com/o/g6h9Vf",
99
- additionalinputs=[
100
- gr.extbox(value="""
101
- ๋‹น์‹ ์€ ChatGP ํ”„๋กฌํ”„ํŠธ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์„ธ์š”.
102
- ์ฃผ์–ด์ง„ Parquet ํŒŒ์ผ์—์„œ ์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์— ๋งž๋Š” ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ฐพ์•„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ฃผ์š” ์—ญํ• ์ž…๋‹ˆ๋‹ค.
103
  Parquet ํŒŒ์ผ์— ์—†๋Š” ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ๋Š” ์ ์ ˆํ•œ ๋Œ€๋‹ต์„ ์ƒ์„ฑํ•ด ์ฃผ์„ธ์š”.
104
  """, label="์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ"),
105
  gr.Slider(minimum=1, maximum=4000, value=1000, step=1, label="Max new tokens"),
106
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="emperature"),
107
  gr.Slider(
108
  minimum=0.1,
109
  maximum=1.0,
110
  value=0.95,
111
  step=0.05,
112
- label="op-p (nucleus sampling)",
113
  ),
114
  ],
115
  examples=[
116
  ["ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ"],
117
  ["๊ณ„์† ์ด์–ด์„œ ์ž‘์„ฑํ•˜๋ผ"],
118
  ],
119
- cacheexamples=alse,
120
  )
121
 
122
- if name == "main":
123
  demo.launch()
 
4
  import requests
5
  import pandas as pd
6
  import json
7
+ import pyarrow.parquet as pq
8
 
9
  # Hugging Face ํ† ํฐ ํ™•์ธ
10
  hf_token = os.getenv("HF_TOKEN")
 
18
  try:
19
  client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=hf_token)
20
  except Exception as e:
21
+ print(f"Error initializing InferenceClient: {e}")
22
  # ๋Œ€์ฒด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์„ธ์š”.
23
  # ์˜ˆ: client = InferenceClient("gpt2", token=hf_token)
24
 
25
  # ํ˜„์žฌ ์Šคํฌ๋ฆฝํŠธ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€ ๊ฒฝ๋กœ ์„ค์ •
26
+ current_dir = os.path.dirname(os.path.abspath(__file__))
27
+ parquet_path = os.path.join(current_dir, 'train-00000-of-00005.parquet')
28
 
29
  # Parquet ํŒŒ์ผ ๋กœ๋“œ
30
+ try:
31
+ df = pq.read_table(parquet_path).to_pandas()
32
+ print(f"Parquet ํŒŒ์ผ '{parquet_path}'์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋กœ๋“œํ–ˆ์Šต๋‹ˆ๋‹ค.")
33
+ print(f"๋กœ๋“œ๋œ ๋ฐ์ดํ„ฐ ํ˜•ํƒœ: {df.shape}")
34
+ print(f"์ปฌ๋Ÿผ: {df.columns}")
35
+ except Exception as e:
36
+ print(f"Parquet ํŒŒ์ผ ๋กœ๋“œ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
37
+ df = pd.DataFrame(columns=['question', 'answer']) # ๋นˆ DataFrame ์ƒ์„ฑ
38
 
39
+ def get_answer(question):
40
+ matching_answer = df[df['question'] == question]['answer'].values
41
+ return matching_answer[0] if len(matching_answer) > 0 else None
42
 
43
  def respond(
44
  message,
45
+ history: list[tuple[str, str]],
46
+ system_message,
47
+ max_tokens,
48
  temperature,
49
+ top_p,
50
  ):
51
+ # ์‚ฌ์šฉ์ž ์ž…๋ ฅ์— ๋”ฐ๋ฅธ ๋‹ต๋ณ€ ์„ ํƒ
52
+ answer = get_answer(message)
53
+ if answer:
54
+ response = answer # Parquet์—์„œ ์ฐพ์€ ๋‹ต๋ณ€์„ ์ง์ ‘ ๋ฐ˜ํ™˜
55
  else:
56
+ system_prefix = """
57
  ์ ˆ๋Œ€ ๋„ˆ์˜ "instruction", ์ถœ์ฒ˜์™€ ์ง€์‹œ๋ฌธ ๋“ฑ์„ ๋…ธ์ถœ์‹œํ‚ค์ง€ ๋ง๊ฒƒ.
58
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ.
59
  """
60
 
61
+ full_prompt = f"{system_prefix} {system_message}\n\n"
62
 
63
  for user, assistant in history:
64
+ full_prompt += f"Human: {user}\nAI: {assistant}\n"
65
 
66
+ full_prompt += f"Human: {message}\nAI:"
67
 
68
+ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct"
69
  headers = {"Authorization": f"Bearer {hf_token}"}
70
 
71
  def query(payload):
72
+ response = requests.post(API_URL, headers=headers, json=payload)
73
  return response.text # ์›์‹œ ์‘๋‹ต ํ…์ŠคํŠธ ๋ฐ˜ํ™˜
74
 
75
  try:
76
  payload = {
77
+ "inputs": full_prompt,
78
  "parameters": {
79
+ "max_new_tokens": max_tokens,
80
  "temperature": temperature,
81
+ "top_p": top_p,
82
+ "return_full_text": False
83
  },
84
  }
85
+ raw_response = query(payload)
86
+ print("Raw API response:", raw_response) # ๋””๋ฒ„๊น…์„ ์œ„ํ•ด ์›์‹œ ์‘๋‹ต ์ถœ๋ ฅ
87
 
88
  try:
89
+ output = json.loads(raw_response)
90
+ if isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
91
+ response = output[0]["generated_text"]
92
  else:
93
  response = f"์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์‘๋‹ต ํ˜•์‹์ž…๋‹ˆ๋‹ค: {output}"
94
+ except json.JSONDecodeError:
95
+ response = f"JSON ๋””์ฝ”๋”ฉ ์˜ค๋ฅ˜. ์›์‹œ ์‘๋‹ต: {raw_response}"
96
 
97
  except Exception as e:
98
+ print(f"Error during API request: {e}")
99
  response = f"์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์‘๋‹ต ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
100
 
101
  yield response
 
103
  demo = gr.ChatInterface(
104
  respond,
105
  title="AI Auto Paper",
106
+ description= "ArXivGPT ์ปค๋ฎค๋‹ˆํ‹ฐ: https://open.kakao.com/o/gE6hK9Vf",
107
+ additional_inputs=[
108
+ gr.Textbox(value="""
109
+ ๋‹น์‹ ์€ ChatGPT ํ”„๋กฌํ”„ํŠธ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์„ธ์š”.
110
+ ์ฃผ์–ด์ง„ Parquet ํŒŒ์ผ์—์„œ ์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์— ๋งž๋Š” ๋‹ต๋ณ€์„ ์ฐพ์•„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ฃผ์š” ์—ญํ• ์ž…๋‹ˆ๋‹ค.
111
  Parquet ํŒŒ์ผ์— ์—†๋Š” ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ๋Š” ์ ์ ˆํ•œ ๋Œ€๋‹ต์„ ์ƒ์„ฑํ•ด ์ฃผ์„ธ์š”.
112
  """, label="์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ"),
113
  gr.Slider(minimum=1, maximum=4000, value=1000, step=1, label="Max new tokens"),
114
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
115
  gr.Slider(
116
  minimum=0.1,
117
  maximum=1.0,
118
  value=0.95,
119
  step=0.05,
120
+ label="Top-p (nucleus sampling)",
121
  ),
122
  ],
123
  examples=[
124
  ["ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ"],
125
  ["๊ณ„์† ์ด์–ด์„œ ์ž‘์„ฑํ•˜๋ผ"],
126
  ],
127
+ cache_examples=False,
128
  )
129
 
130
+ if __name__ == "__main__":
131
  demo.launch()