File size: 7,927 Bytes
f9b9d56
83ee74c
574f73e
705c5b5
83ee74c
f9b9d56
705c5b5
 
f9b9d56
705c5b5
0997082
 
705c5b5
 
 
 
 
 
 
 
 
 
 
0997082
705c5b5
 
83ee74c
705c5b5
 
 
 
 
 
 
 
83ee74c
705c5b5
 
 
 
 
 
f2c0975
705c5b5
 
 
 
0997082
705c5b5
 
 
 
0997082
705c5b5
 
 
 
 
 
 
 
 
 
 
0997082
705c5b5
83ee74c
0997082
705c5b5
f2c0975
 
83ee74c
0997082
 
83ee74c
705c5b5
 
 
 
0997082
705c5b5
 
7b3fa19
 
 
 
 
 
 
63c5e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9b9d56
 
63c5e29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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)}"

css = """
footer {
    visibility: hidden;
}
"""


# ... (이전 import λ¬Έκ³Ό ν•¨μˆ˜λ“€μ€ 동일)

with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
    with gr.Row():
        with gr.Column(scale=2):
            chatbot = gr.Chatbot()
            msg = gr.Textbox(label="λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”")
            clear = gr.ClearButton([msg, chatbot])
        
        with gr.Column(scale=1):
            with gr.Group():
                fashion_file = gr.File(label="Fashion Code File", file_types=[".cod", ".txt", ".py"])
                fashion_analyze = gr.Button("νŒ¨μ…˜ μ½”λ“œ 뢄석")
                
                uhd_file = gr.File(label="UHD Image Code File", file_types=[".cod", ".txt", ".py"])
                uhd_analyze = gr.Button("UHD 이미지 μ½”λ“œ 뢄석")
                
                mixgen_file = gr.File(label="MixGEN Code File", file_types=[".cod", ".txt", ".py"])
                mixgen_analyze = gr.Button("MixGEN μ½”λ“œ 뢄석")
                
                parquet_file = gr.File(label="Parquet File", file_types=[".parquet"])
                parquet_analyze = gr.Button("Parquet 파일 뢄석")
            
            with gr.Accordion("κ³ κΈ‰ μ„€μ •", open=False):
                system_message = gr.Textbox(label="System Message", value="")
                max_tokens = gr.Slider(minimum=1, maximum=8000, value=4000, label="Max Tokens")
                temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
                top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")

    # 뢄석 λ²„νŠΌ 클릭 이벀트 ν•Έλ“€λŸ¬
    def analyze_file(file_type):
        if file_type == "fashion":
            return "νŒ¨μ…˜ μ½”λ“œ μ‹€ν–‰"
        elif file_type == "uhd":
            return "UHD 이미지 μ½”λ“œ μ‹€ν–‰"
        elif file_type == "mixgen":
            return "MixGEN μ½”λ“œ μ‹€ν–‰"
        elif file_type == "parquet":
            return "test.parquet μ‹€ν–‰"

    # μ±„νŒ… 제좜 ν•Έλ“€λŸ¬
    def chat(message, history):
        return respond(
            message=message,
            history=history,
            fashion_file=fashion_file.value,
            uhd_file=uhd_file.value,
            mixgen_file=mixgen_file.value,
            parquet_file=parquet_file.value,
            system_message=system_message.value,
            max_tokens=max_tokens.value,
            temperature=temperature.value,
            top_p=top_p.value,
        )

    # 이벀트 바인딩
    msg.submit(chat, [msg, chatbot], [msg, chatbot])
    fashion_analyze.click(lambda: analyze_file("fashion"), None, msg)
    uhd_analyze.click(lambda: analyze_file("uhd"), None, msg)
    mixgen_analyze.click(lambda: analyze_file("mixgen"), None, msg)
    parquet_analyze.click(lambda: analyze_file("parquet"), None, msg)

    # 예제 μΆ”κ°€
    gr.Examples(
        examples=[
            ["μƒμ„Έν•œ μ‚¬μš© 방법을 마치 화면을 λ³΄λ©΄μ„œ μ„€λͺ…ν•˜λ“―이 4000 토큰 이상 μžμ„Ένžˆ μ„€λͺ…ν•˜λΌ"],
            ["FAQ 20건을 μƒμ„Έν•˜κ²Œ μž‘μ„±ν•˜λΌ. 4000토큰 이상 μ‚¬μš©ν•˜λΌ."],
            ["μ‚¬μš© 방법과 차별점, νŠΉμ§•, 강점을 μ€‘μ‹¬μœΌλ‘œ 4000 토큰 이상 유튜브 μ˜μƒ 슀크립트 ν˜•νƒœλ‘œ μž‘μ„±ν•˜λΌ"],
            ["λ³Έ μ„œλΉ„μŠ€λ₯Ό SEO μ΅œμ ν™”ν•˜μ—¬ λΈ”λ‘œκ·Έ 포슀트둜 4000 토큰 이상 μž‘μ„±ν•˜λΌ"],
            ["νŠΉν—ˆ μΆœμ›μ— ν™œμš©ν•  기술 및 λΉ„μ¦ˆλ‹ˆμŠ€λͺ¨λΈ 츑면을 ν¬ν•¨ν•˜μ—¬ νŠΉν—ˆ μΆœμ›μ„œ ꡬ성에 맞게 μž‘μ„±ν•˜λΌ"],
            ["계속 μ΄μ–΄μ„œ λ‹΅λ³€ν•˜λΌ"],
        ],
        inputs=msg,
    )

if __name__ == "__main__":
    demo.launch()