Spaces:
Raven7
/
Runtime error

LLM2 / app.py
Raven7's picture
Update app.py
f3ae789 verified
raw
history blame
4.19 kB
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"였λ₯˜: 응닡 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}")
response = f"μ£„μ†‘ν•©λ‹ˆλ‹€. 응닡 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
demo = gr.ChatInterface(
respond,
title="My Chatbot",
description="This is my chatbot!",
additional_inputs=[
gr.Textbox(value="""
당신은 μ±—λ΄‡μž…λ‹ˆλ‹€. λͺ¨λ“  μ§ˆλ¬Έμ— λŒ€ν•΄ μΉœμ ˆν•˜κ³  μ •ν™•ν•œ 닡변을 μ œκ³΅ν•˜μ„Έμš”.
μ§ˆλ¬Έμ— λŒ€ν•œ 닡변을 찾을 수 μ—†λŠ” 경우, μ μ ˆν•œ λŒ€μ•ˆμ„ μ œκ³΅ν•΄ μ£Όμ„Έμš”.
""", label="μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ"),
gr.Slider(minimum=1, maximum=4000, value=2000, step=1, label="μ΅œλŒ€ 토큰 수"),
gr.Slider(minimum=0.1, maximum=4.0, value=1.0, step=0.1, label="μ˜¨λ„"),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="top-p (핡심 μƒ˜ν”Œλ§)",
),
],
examples=[
["μ•ˆλ…•"],
["계속 μ΄μ–΄μ„œ μž‘μ„±ν•΄λ΄"],
],
cache_examples=False,
)
if __name__ == "__main__":
demo.launch()