Spaces:
Runtime error
Runtime error
import gradio as gr | |
from huggingfacehub import InferenceClient, HfApi | |
import os | |
import requests | |
import pandas as pd | |
import json | |
# Hugging Face ν ν° νμΈ | |
hftoken = os.getenv("H") | |
if not hftoken: | |
raise ValueError("H νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.") | |
# λͺ¨λΈ μ 보 νμΈ | |
api = HfApi(token=hftoken) | |
try: | |
client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token="H") | |
except Exception as e: | |
print(f"rror initializing InferenceClient: {e}") | |
# λ체 λͺ¨λΈμ μ¬μ©νκ±°λ μ€λ₯ μ²λ¦¬λ₯Ό μννμΈμ. | |
# μ: client = InferenceClient("gpt2", token=hftoken) | |
# νμ¬ μ€ν¬λ¦½νΈμ λλ ν 리λ₯Ό κΈ°μ€μΌλ‘ μλ κ²½λ‘ μ€μ | |
currentdir = os.path.dirname(os.path.abspath(file)) | |
csvpath = os.path.join(currentdir, 'prompts.csv') | |
datapath = os.path.join(currentdir, 'newdataset.parquet') | |
# CSV νμΌ λ‘λ | |
promptsdf = pd.readcsv(csvpath) | |
datadf = pd.readparquet(datapath) | |
def getprompt(act): | |
matchingprompt = promptsdf[promptsdf['act'] == act]['prompt'].values | |
return matchingprompt[0] if len(matchingprompt) 0 else None | |
def respond( | |
message, | |
history: list[tuple[str, str]], | |
systemmessage, | |
maxtokens, | |
temperature, | |
topp, | |
): | |
# μ¬μ©μ μ λ ₯μ λ°λ₯Έ ν둬ννΈ μ ν | |
prompt = getprompt(message) | |
if prompt: | |
response = prompt # CSVμμ μ°Ύμ ν둬ννΈλ₯Ό μ§μ λ°ν | |
else: | |
systemprefix = """ | |
λΉμ μ μ±λ΄μ λλ€. λͺ¨λ μ§λ¬Έμ λν΄ μΉμ νκ³ μ νν λ΅λ³μ μ 곡νμΈμ. | |
μ§λ¬Έμ λν λ΅λ³μ μ°Ύμ μ μλ κ²½μ°, μ μ ν λμμ μ κ³΅ν΄ μ£ΌμΈμ. | |
""" | |
fullprompt = f"{systemprefix} {systemmessage}\n\n" | |
for user, assistant in history: | |
fullprompt += f"Human: {user}\nAI: {assistant}\n" | |
fullprompt += f"Human: {message}\nAI:" | |
APIL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct" | |
headers = {"Authorization": f"Bearer {hftoken}"} | |
def query(payload): | |
response = requests.post(APIL, headers=headers, json=payload) | |
return response.text # μμ μλ΅ ν μ€νΈ λ°ν | |
try: | |
payload = { | |
"inputs": fullprompt, | |
"parameters": { | |
"maxnewtokens": maxtokens, | |
"temperature": temperature, | |
"topp": topp, | |
"returnfulltext": False | |
}, | |
} | |
rawresponse = query(payload) | |
print("aw API response:", rawresponse) # λλ²κΉ μ μν΄ μμ μλ΅ μΆλ ₯ | |
try: | |
output = json.loads(rawresponse) | |
if isinstance(output, list) and len(output) 0 and "generatedtext" in output[0]: | |
response = output[0]["generatedtext"] | |
else: | |
response = f"μμμΉ λͺ»ν μλ΅ νμμ λλ€: {output}" | |
except json.JSecoderror: | |
response = f"JS λμ½λ© μ€λ₯. μμ μλ΅: {rawresponse}" | |
except Exception as e: | |
print(f"rror during API request: {e}") | |
response = f"μ£μ‘ν©λλ€. μλ΅ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}" | |
yield response | |
demo = gr.ChatInterface( | |
respond, | |
title="My Chatbot", | |
description= "this is my chatbot!", | |
additionalinputs=[ | |
gr.extbox(value=""" | |
λΉμ μ μ±λ΄μ λλ€. λͺ¨λ μ§λ¬Έμ λν΄ μΉμ νκ³ μ νν λ΅λ³μ μ 곡νμΈμ. | |
μ§λ¬Έμ λν λ΅λ³μ μ°Ύμ μ μλ κ²½μ°, μ μ ν λμμ μ κ³΅ν΄ μ£ΌμΈμ. | |
""", label="μμ€ν ν둬ννΈ"), | |
gr.Slider(minimum=1, maximum=4000, value=2000, step=1, label="Max new tokens"), | |
gr.Slider(minimum=0.1, maximum=4.0, value=1.0, step=0.1, label="temperature"), | |
gr.Slider( | |
minimum=0.1, | |
maximum=1.0, | |
value=0.95, | |
step=0.05, | |
label="top-p (nucleus sampling)", | |
), | |
], | |
examples=[ | |
["νκΈλ‘ λ΅λ³ν κ²"], | |
["κ³μ μ΄μ΄μ μμ±νλΌ"], | |
], | |
cacheexamples=alse, | |
) | |
if name == "main": | |
demo.launch() |