Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,123 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import pandas as pd
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def respond(
|
| 13 |
message,
|
| 14 |
-
history,
|
| 15 |
-
|
| 16 |
-
|
| 17 |
temperature,
|
| 18 |
-
|
| 19 |
-
prompts,
|
| 20 |
):
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
messages.append({"role": "user", "content": val[0]})
|
| 26 |
-
if val[1]:
|
| 27 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
demo = gr.ChatInterface(
|
| 48 |
respond,
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
gr.Slider(
|
| 54 |
minimum=0.1,
|
| 55 |
maximum=1.0,
|
| 56 |
value=0.95,
|
| 57 |
step=0.05,
|
| 58 |
-
label="
|
| 59 |
),
|
| 60 |
],
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
-
if
|
| 65 |
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()
|