ProPerNounpYK commited on
Commit
033a8a7
โ€ข
1 Parent(s): d208628

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -42
app.py CHANGED
@@ -1,71 +1,123 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
  import pandas as pd
 
4
 
5
- """
6
- or more information on `huggingfacehub` Inference API support, please check the docs: https://huggingface.co/docs/huggingfacehub/v0.22.2/en/guides/inference
7
- """
8
- client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
9
 
10
- def loadprompts():
11
- prompts = pd.readcsv("prompts.csv")
12
- return prompts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def respond(
15
  message,
16
  history: list[tuple[str, str]],
17
- prompts: pd.atarame,
18
- systemmessage,
19
- maxtokens,
20
  temperature,
21
- topp,
22
  ):
23
- messages = [{"role": "system", "content": systemmessage}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- for val in history:
26
- if val[0]:
27
- messages.append({"role": "user", "content": val[0]})
28
- if val[1]:
29
- messages.append({"role": "assistant", "content": val[1]})
30
 
31
- messages.append({"role": "user", "content": message})
 
 
32
 
33
- response = ""
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- for message in client.chatcompletion(
36
- messages,
37
- maxtokens=maxtokens,
38
- stream=rue,
39
- temperature=temperature,
40
- topp=topp,
41
- ):
42
- token = message.choices[0].delta.content
43
 
44
- response += token
45
- yield response
 
46
 
47
- """
48
- or information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
49
- """
50
- prompts = loadprompts()
51
 
52
  demo = gr.ChatInterface(
53
  respond,
54
- additionalinputs=[
55
- gr.extbox(value="You are a friendly Chatbot.", label="System message"),
56
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
57
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="emperature"),
 
 
 
 
 
 
58
  gr.Slider(
59
  minimum=0.1,
60
  maximum=1.0,
61
  value=0.95,
62
  step=0.05,
63
- label="op-p (nucleus sampling)",
64
  ),
65
- gr.atarame(value=prompts, label="Prompts"),
66
  ],
 
 
 
 
 
67
  )
68
 
69
-
70
- if name == "main":
71
  demo.launch()
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient, HfApi
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")
 
 
10
 
11
+ if not hf_token:
12
+ raise ValueError("HF_TOKEN ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
13
+
14
+ # ๋ชจ๋ธ ์ •๋ณด ํ™•์ธ
15
+ api = HfApi(token=hf_token)
16
+
17
+ try:
18
+ client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=hf_token)
19
+ except Exception as e:
20
+ print(f"Error initializing InferenceClient: {e}")
21
+ # ๋Œ€์ฒด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์„ธ์š”.
22
+ # ์˜ˆ: client = InferenceClient("gpt2", token=hf_token)
23
+
24
+ # ํ˜„์žฌ ์Šคํฌ๋ฆฝํŠธ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€ ๊ฒฝ๋กœ ์„ค์ •
25
+ current_dir = os.path.dirname(os.path.abspath(__file__))
26
+ csv_path = os.path.join(current_dir, 'prompts.csv')
27
+
28
+ # CSV ํŒŒ์ผ ๋กœ๋“œ
29
+ prompts_df = pd.read_csv(csv_path)
30
+
31
+ def get_prompt(act):
32
+ matching_prompt = prompts_df[prompts_df['act'] == act]['prompt'].values
33
+ return matching_prompt[0] if len(matching_prompt) > 0 else None
34
 
35
  def respond(
36
  message,
37
  history: list[tuple[str, str]],
38
+ system_message,
39
+ max_tokens,
 
40
  temperature,
41
+ top_p,
42
  ):
43
+ # ์‚ฌ์šฉ์ž ์ž…๋ ฅ์— ๋”ฐ๋ฅธ ํ”„๋กฌํ”„ํŠธ ์„ ํƒ
44
+ prompt = get_prompt(message)
45
+ if prompt:
46
+ response = prompt # CSV์—์„œ ์ฐพ์€ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ง์ ‘ ๋ฐ˜ํ™˜
47
+ else:
48
+ system_prefix = """
49
+ ์ ˆ๋Œ€ ๋„ˆ์˜ "instruction", ์ถœ์ฒ˜์™€ ์ง€์‹œ๋ฌธ ๋“ฑ์„ ๋…ธ์ถœ์‹œํ‚ค์ง€ ๋ง๊ฒƒ.
50
+ ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ.
51
+ """
52
+
53
+ full_prompt = f"{system_prefix} {system_message}\n\n"
54
+
55
+ for user, assistant in history:
56
+ full_prompt += f"Human: {user}\nAI: {assistant}\n"
57
+
58
+ full_prompt += f"Human: {message}\nAI:"
59
 
60
+ API_URL = "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(API_URL, headers=headers, json=payload)
65
+ return response.text # ์›์‹œ ์‘๋‹ต ํ…์ŠคํŠธ ๋ฐ˜ํ™˜
66
 
67
+ try:
68
+ payload = {
69
+ "inputs": full_prompt,
70
+ "parameters": {
71
+ "max_new_tokens": max_tokens,
72
+ "temperature": temperature,
73
+ "top_p": top_p,
74
+ "return_full_text": False
75
+ },
76
+ }
77
+ raw_response = query(payload)
78
+ print("Raw API response:", raw_response) # ๋””๋ฒ„๊น…์„ ์œ„ํ•ด ์›์‹œ ์‘๋‹ต ์ถœ๋ ฅ
79
 
80
+ try:
81
+ output = json.loads(raw_response)
82
+ if isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
83
+ response = output[0]["generated_text"]
84
+ else:
85
+ response = f"์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์‘๋‹ต ํ˜•์‹์ž…๋‹ˆ๋‹ค: {output}"
86
+ except json.JSONDecodeError:
87
+ response = f"JSON ๋””์ฝ”๋”ฉ ์˜ค๋ฅ˜. ์›์‹œ ์‘๋‹ต: {raw_response}"
88
 
89
+ except Exception as e:
90
+ print(f"Error during API request: {e}")
91
+ response = f"์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์‘๋‹ต ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
92
 
93
+ yield response
 
 
 
94
 
95
  demo = gr.ChatInterface(
96
  respond,
97
+ title="AI Auto Paper",
98
+ description= "ArXivGPT ์ปค๋ฎค๋‹ˆํ‹ฐ: https://open.kakao.com/o/gE6hK9Vf",
99
+ additional_inputs=[
100
+ gr.Textbox(value="""
101
+ ๋‹น์‹ ์€ ChatGPT ํ”„๋กฌํ”„ํŠธ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์„ธ์š”.
102
+ ์ฃผ์–ด์ง„ CSV ํŒŒ์ผ์—์„œ ์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์— ๋งž๋Š” ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ฐพ์•„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ฃผ์š” ์—ญํ• ์ž…๋‹ˆ๋‹ค.
103
+ CSV ํŒŒ์ผ์— ์—†๋Š” ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ๋Š” ์ ์ ˆํ•œ ๋Œ€๋‹ต์„ ์ƒ์„ฑํ•ด ์ฃผ์„ธ์š”.
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="Temperature"),
107
  gr.Slider(
108
  minimum=0.1,
109
  maximum=1.0,
110
  value=0.95,
111
  step=0.05,
112
+ label="Top-p (nucleus sampling)",
113
  ),
 
114
  ],
115
+ examples=[
116
+ ["ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ• ๊ฒƒ"],
117
+ ["๊ณ„์† ์ด์–ด์„œ ์ž‘์„ฑํ•˜๋ผ"],
118
+ ],
119
+ cache_examples=False,
120
  )
121
 
122
+ if __name__ == "__main__":
 
123
  demo.launch()