ginichat / app.py
openfree's picture
Update app.py
705c5b5 verified
raw
history blame
5.89 kB
import gradio as gr
from huggingface_hub import InferenceClient
import os
import pandas as pd
from typing import List, Tuple
# μΆ”λ‘  API ν΄λΌμ΄μ–ΈνŠΈ μ„€μ •
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
def read_uploaded_file(file):
if file is None:
return ""
try:
if file.name.endswith('.parquet'):
df = pd.read_parquet(file.name, engine='pyarrow')
return df.head(10).to_markdown(index=False)
else:
content = file.read()
if isinstance(content, bytes):
return content.decode('utf-8')
return content
except Exception as e:
return f"νŒŒμΌμ„ μ½λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
def respond(
message,
history: List[Tuple[str, str]],
fashion_file, # 파일 μ—…λ‘œλ“œ μž…λ ₯
uhd_file, # 파일 μ—…λ‘œλ“œ μž…λ ₯
mixgen_file, # 파일 μ—…λ‘œλ“œ μž…λ ₯
parquet_file, # 파일 μ—…λ‘œλ“œ μž…λ ₯
system_message="",
max_tokens=1024,
temperature=0.7,
top_p=0.9,
):
system_prefix = """λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ 닡변할것. λ„ˆλŠ” 주어진 μ†ŒμŠ€μ½”λ“œλ₯Ό 기반으둜 "μ„œλΉ„μŠ€ μ‚¬μš© μ„€λͺ… 및 μ•ˆλ‚΄, Q&Aλ₯Ό ν•˜λŠ” 역할이닀". μ•„μ£Ό μΉœμ ˆν•˜κ³  μžμ„Έν•˜κ²Œ 4000토큰 이상 Markdown ν˜•μ‹μœΌλ‘œ μž‘μ„±ν•˜λΌ. λ„ˆλŠ” μ½”λ“œλ₯Ό 기반으둜 μ‚¬μš© μ„€λͺ… 및 질의 응닡을 μ§„ν–‰ν•˜λ©°, μ΄μš©μžμ—κ²Œ 도움을 μ£Όμ–΄μ•Ό ν•œλ‹€. μ΄μš©μžκ°€ κΆκΈˆν•΄ ν•  λ§Œν•œ λ‚΄μš©μ— μΉœμ ˆν•˜κ²Œ μ•Œλ €μ£Όλ„λ‘ ν•˜λΌ. μ½”λ“œ 전체 λ‚΄μš©μ— λŒ€ν•΄μ„œλŠ” λ³΄μ•ˆμ„ μœ μ§€ν•˜κ³ , ν‚€ κ°’ 및 μ—”λ“œν¬μΈνŠΈμ™€ ꡬ체적인 λͺ¨λΈμ€ κ³΅κ°œν•˜μ§€ 마라."""
if message.lower() == "νŒ¨μ…˜ μ½”λ“œ μ‹€ν–‰" and fashion_file is not None:
fashion_content = read_uploaded_file(fashion_file)
system_message += f"\n\nνŒ¨μ…˜ μ½”λ“œ λ‚΄μš©:\n```python\n{fashion_content}\n```"
message = "νŒ¨μ…˜ κ°€μƒν”ΌνŒ…μ— λŒ€ν•œ λ‚΄μš©μ„ ν•™μŠ΅ν•˜μ˜€κ³ , μ„€λͺ…ν•  μ€€λΉ„κ°€ λ˜μ–΄μžˆλ‹€κ³  μ•Œλ¦¬κ³  μ„œλΉ„μŠ€ URL(https://aiqcamp-fash.hf.space)을 톡해 ν…ŒμŠ€νŠΈ 해보라고 좜λ ₯ν•˜λΌ."
elif message.lower() == "uhd 이미지 μ½”λ“œ μ‹€ν–‰" and uhd_file is not None:
uhd_content = read_uploaded_file(uhd_file)
system_message += f"\n\nUHD 이미지 μ½”λ“œ λ‚΄μš©:\n```python\n{uhd_content}\n```"
message = "UHD 이미지 생성에 λŒ€ν•œ λ‚΄μš©μ„ ν•™μŠ΅ν•˜μ˜€κ³ , μ„€λͺ…ν•  μ€€λΉ„κ°€ λ˜μ–΄μžˆλ‹€κ³  μ•Œλ¦¬κ³  μ„œλΉ„μŠ€ URL(https://openfree-ultpixgen.hf.space)을 톡해 ν…ŒμŠ€νŠΈ 해보라고 좜λ ₯ν•˜λΌ."
elif message.lower() == "mixgen μ½”λ“œ μ‹€ν–‰" and mixgen_file is not None:
mixgen_content = read_uploaded_file(mixgen_file)
system_message += f"\n\nMixGEN μ½”λ“œ λ‚΄μš©:\n```python\n{mixgen_content}\n```"
message = "MixGEN3 이미지 생성에 λŒ€ν•œ λ‚΄μš©μ„ ν•™μŠ΅ν•˜μ˜€κ³ , μ„€λͺ…ν•  μ€€λΉ„κ°€ λ˜μ–΄μžˆλ‹€κ³  μ•Œλ¦¬κ³  μ„œλΉ„μŠ€ URL(https://openfree-mixgen3.hf.space)을 톡해 ν…ŒμŠ€νŠΈ 해보라고 좜λ ₯ν•˜λΌ."
elif message.lower() == "test.parquet μ‹€ν–‰" and parquet_file is not None:
parquet_content = read_uploaded_file(parquet_file)
system_message += f"\n\ntest.parquet 파일 λ‚΄μš©:\n```markdown\n{parquet_content}\n```"
message = "test.parquet νŒŒμΌμ— λŒ€ν•œ λ‚΄μš©μ„ ν•™μŠ΅ν•˜μ˜€κ³ , κ΄€λ ¨ μ„€λͺ… 및 Q&Aλ₯Ό 진행할 μ€€λΉ„κ°€ λ˜μ–΄μžˆλ‹€. κΆκΈˆν•œ 점이 있으면 물어보라."
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
response = ""
try:
for message in hf_client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.get('content', None)
if token:
response += token
yield response
except Exception as e:
yield f"μΆ”λ‘  쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.File(label="Fashion Code File", file_types=[".cod", ".txt", ".py"]),
gr.File(label="UHD Image Code File", file_types=[".cod", ".txt", ".py"]),
gr.File(label="MixGEN Code File", file_types=[".cod", ".txt", ".py"]),
gr.File(label="Parquet File", file_types=[".parquet"]),
gr.Textbox(label="System Message", value=""),
gr.Slider(minimum=1, maximum=8000, value=4000, label="Max Tokens"),
gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature"),
gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P"),
],
examples=[
["νŒ¨μ…˜ μ½”λ“œ μ‹€ν–‰"],
["UHD 이미지 μ½”λ“œ μ‹€ν–‰"],
["MixGEN μ½”λ“œ μ‹€ν–‰"],
["test.parquet μ‹€ν–‰"],
["μƒμ„Έν•œ μ‚¬μš© 방법을 마치 화면을 λ³΄λ©΄μ„œ μ„€λͺ…ν•˜λ“―이 4000 토큰 이상 μžμ„Ένžˆ μ„€λͺ…ν•˜λΌ"],
["FAQ 20건을 μƒμ„Έν•˜κ²Œ μž‘μ„±ν•˜λΌ. 4000토큰 이상 μ‚¬μš©ν•˜λΌ."],
["μ‚¬μš© 방법과 차별점, νŠΉμ§•, 강점을 μ€‘μ‹¬μœΌλ‘œ 4000 토큰 이상 유튜브 μ˜μƒ 슀크립트 ν˜•νƒœλ‘œ μž‘μ„±ν•˜λΌ"],
["λ³Έ μ„œλΉ„μŠ€λ₯Ό SEO μ΅œμ ν™”ν•˜μ—¬ λΈ”λ‘œκ·Έ 포슀트둜 4000 토큰 이상 μž‘μ„±ν•˜λΌ"],
["νŠΉν—ˆ μΆœμ›μ— ν™œμš©ν•  기술 및 λΉ„μ¦ˆλ‹ˆμŠ€λͺ¨λΈ 츑면을 ν¬ν•¨ν•˜μ—¬ νŠΉν—ˆ μΆœμ›μ„œ ꡬ성에 맞게 μž‘μ„±ν•˜λΌ"],
["계속 μ΄μ–΄μ„œ λ‹΅λ³€ν•˜λΌ"],
],
theme="Nymbo/Nymbo_Theme",
cache_examples=False,
)
if __name__ == "__main__":
demo.launch()