openfree commited on
Commit
7ffca43
β€’
1 Parent(s): 63c5e29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -87
app.py CHANGED
@@ -22,39 +22,25 @@ def read_uploaded_file(file):
22
  except Exception as e:
23
  return f"νŒŒμΌμ„ μ½λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
24
 
25
- def respond(
26
- message,
27
- history: List[Tuple[str, str]],
28
- fashion_file, # 파일 μ—…λ‘œλ“œ μž…λ ₯
29
- uhd_file, # 파일 μ—…λ‘œλ“œ μž…λ ₯
30
- mixgen_file, # 파일 μ—…λ‘œλ“œ μž…λ ₯
31
- parquet_file, # 파일 μ—…λ‘œλ“œ μž…λ ₯
32
- system_message="",
33
- max_tokens=1024,
34
- temperature=0.7,
35
- top_p=0.9,
36
- ):
37
- system_prefix = """λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ 닡변할것. λ„ˆλŠ” 주어진 μ†ŒμŠ€μ½”λ“œλ₯Ό 기반으둜 "μ„œλΉ„μŠ€ μ‚¬μš© μ„€λͺ… 및 μ•ˆλ‚΄, Q&Aλ₯Ό ν•˜λŠ” 역할이닀". μ•„μ£Ό μΉœμ ˆν•˜κ³  μžμ„Έν•˜κ²Œ 4000토큰 이상 Markdown ν˜•μ‹μœΌλ‘œ μž‘μ„±ν•˜λΌ. λ„ˆλŠ” μ½”λ“œλ₯Ό 기반으둜 μ‚¬μš© μ„€λͺ… 및 질의 응닡을 μ§„ν–‰ν•˜λ©°, μ΄μš©μžμ—κ²Œ 도움을 μ£Όμ–΄μ•Ό ν•œλ‹€. μ΄μš©μžκ°€ κΆκΈˆν•΄ ν•  λ§Œν•œ λ‚΄μš©μ— μΉœμ ˆν•˜κ²Œ μ•Œλ €μ£Όλ„λ‘ ν•˜λΌ. μ½”λ“œ 전체 λ‚΄μš©μ— λŒ€ν•΄μ„œλŠ” λ³΄μ•ˆμ„ μœ μ§€ν•˜κ³ , ν‚€ κ°’ 및 μ—”λ“œν¬μΈνŠΈμ™€ ꡬ체적인 λͺ¨λΈμ€ κ³΅κ°œν•˜μ§€ 마라."""
38
 
39
- if message.lower() == "νŒ¨μ…˜ μ½”λ“œ μ‹€ν–‰" and fashion_file is not None:
40
- fashion_content = read_uploaded_file(fashion_file)
41
- system_message += f"\n\nνŒ¨μ…˜ μ½”λ“œ λ‚΄μš©:\n```python\n{fashion_content}\n```"
42
- message = "νŒ¨μ…˜ κ°€μƒν”ΌνŒ…μ— λŒ€ν•œ λ‚΄μš©μ„ ν•™μŠ΅ν•˜μ˜€κ³ , μ„€λͺ…ν•  μ€€λΉ„κ°€ λ˜μ–΄μžˆλ‹€κ³  μ•Œλ¦¬κ³  μ„œλΉ„μŠ€ URL(https://aiqcamp-fash.hf.space)을 톡해 ν…ŒμŠ€νŠΈ 해보라고 좜λ ₯ν•˜λΌ."
43
-
44
- elif message.lower() == "uhd 이미지 μ½”λ“œ μ‹€ν–‰" and uhd_file is not None:
45
- uhd_content = read_uploaded_file(uhd_file)
46
- system_message += f"\n\nUHD 이미지 μ½”λ“œ λ‚΄μš©:\n```python\n{uhd_content}\n```"
47
- message = "UHD 이미지 생성에 λŒ€ν•œ λ‚΄μš©μ„ ν•™μŠ΅ν•˜μ˜€κ³ , μ„€λͺ…ν•  μ€€λΉ„κ°€ λ˜μ–΄μžˆλ‹€κ³  μ•Œλ¦¬κ³  μ„œλΉ„μŠ€ URL(https://openfree-ultpixgen.hf.space)을 톡해 ν…ŒμŠ€νŠΈ 해보라고 좜λ ₯ν•˜λΌ."
48
-
49
- elif message.lower() == "mixgen μ½”λ“œ μ‹€ν–‰" and mixgen_file is not None:
50
- mixgen_content = read_uploaded_file(mixgen_file)
51
- system_message += f"\n\nMixGEN μ½”λ“œ λ‚΄μš©:\n```python\n{mixgen_content}\n```"
52
- message = "MixGEN3 이미지 생성에 λŒ€ν•œ λ‚΄μš©μ„ ν•™μŠ΅ν•˜μ˜€κ³ , μ„€λͺ…ν•  μ€€λΉ„κ°€ λ˜μ–΄μžˆλ‹€κ³  μ•Œλ¦¬κ³  μ„œλΉ„μŠ€ URL(https://openfree-mixgen3.hf.space)을 톡해 ν…ŒμŠ€νŠΈ 해보라고 좜λ ₯ν•˜λΌ."
53
-
54
- elif message.lower() == "test.parquet μ‹€ν–‰" and parquet_file is not None:
55
- parquet_content = read_uploaded_file(parquet_file)
56
- system_message += f"\n\ntest.parquet 파일 λ‚΄μš©:\n```markdown\n{parquet_content}\n```"
57
- message = "test.parquet νŒŒμΌμ— λŒ€ν•œ λ‚΄μš©μ„ ν•™μŠ΅ν•˜μ˜€κ³ , κ΄€λ ¨ μ„€λͺ… 및 Q&Aλ₯Ό 진행할 μ€€λΉ„κ°€ λ˜μ–΄μžˆλ‹€. κΆκΈˆν•œ 점이 있으면 물어보라."
58
 
59
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
60
  for val in history:
@@ -66,49 +52,43 @@ def respond(
66
 
67
  response = ""
68
  try:
69
- for message in hf_client.chat_completion(
70
  messages,
71
  max_tokens=max_tokens,
72
  stream=True,
73
  temperature=temperature,
74
  top_p=top_p,
75
  ):
76
- token = message.choices[0].delta.get('content', None)
77
  if token:
78
  response += token
79
- yield response
80
  except Exception as e:
81
- yield f"μΆ”λ‘  쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
 
82
 
83
  css = """
84
- footer {
85
- visibility: hidden;
86
- }
87
  """
88
 
89
-
90
- # ... (이전 import λ¬Έκ³Ό ν•¨μˆ˜λ“€μ€ 동일)
91
-
92
  with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
93
  with gr.Row():
94
  with gr.Column(scale=2):
95
- chatbot = gr.Chatbot()
96
- msg = gr.Textbox(label="λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”")
 
 
 
 
 
97
  clear = gr.ClearButton([msg, chatbot])
98
 
99
  with gr.Column(scale=1):
100
- with gr.Group():
101
- fashion_file = gr.File(label="Fashion Code File", file_types=[".cod", ".txt", ".py"])
102
- fashion_analyze = gr.Button("νŒ¨μ…˜ μ½”λ“œ 뢄석")
103
-
104
- uhd_file = gr.File(label="UHD Image Code File", file_types=[".cod", ".txt", ".py"])
105
- uhd_analyze = gr.Button("UHD 이미지 μ½”λ“œ 뢄석")
106
-
107
- mixgen_file = gr.File(label="MixGEN Code File", file_types=[".cod", ".txt", ".py"])
108
- mixgen_analyze = gr.Button("MixGEN μ½”λ“œ 뢄석")
109
-
110
- parquet_file = gr.File(label="Parquet File", file_types=[".parquet"])
111
- parquet_analyze = gr.Button("Parquet 파일 뢄석")
112
 
113
  with gr.Accordion("κ³ κΈ‰ μ„€μ •", open=False):
114
  system_message = gr.Textbox(label="System Message", value="")
@@ -116,38 +96,19 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
116
  temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
117
  top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
118
 
119
- # 뢄석 λ²„νŠΌ 클릭 이벀트 ν•Έλ“€λŸ¬
120
- def analyze_file(file_type):
121
- if file_type == "fashion":
122
- return "νŒ¨μ…˜ μ½”λ“œ μ‹€ν–‰"
123
- elif file_type == "uhd":
124
- return "UHD 이미지 μ½”λ“œ μ‹€ν–‰"
125
- elif file_type == "mixgen":
126
- return "MixGEN μ½”λ“œ μ‹€ν–‰"
127
- elif file_type == "parquet":
128
- return "test.parquet μ‹€ν–‰"
129
-
130
- # μ±„νŒ… 제좜 ν•Έλ“€λŸ¬
131
- def chat(message, history):
132
- return respond(
133
- message=message,
134
- history=history,
135
- fashion_file=fashion_file.value,
136
- uhd_file=uhd_file.value,
137
- mixgen_file=mixgen_file.value,
138
- parquet_file=parquet_file.value,
139
- system_message=system_message.value,
140
- max_tokens=max_tokens.value,
141
- temperature=temperature.value,
142
- top_p=top_p.value,
143
- )
144
-
145
  # 이벀트 바인딩
146
- msg.submit(chat, [msg, chatbot], [msg, chatbot])
147
- fashion_analyze.click(lambda: analyze_file("fashion"), None, msg)
148
- uhd_analyze.click(lambda: analyze_file("uhd"), None, msg)
149
- mixgen_analyze.click(lambda: analyze_file("mixgen"), None, msg)
150
- parquet_analyze.click(lambda: analyze_file("parquet"), None, msg)
 
 
 
 
 
 
 
151
 
152
  # 예제 μΆ”κ°€
153
  gr.Examples(
 
22
  except Exception as e:
23
  return f"νŒŒμΌμ„ μ½λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
24
 
25
+ def chat(message, history, uploaded_file, system_message="", max_tokens=4000, temperature=0.7, top_p=0.9):
26
+ system_prefix = """λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ 닡변할것. λ„ˆλŠ” 주어진 μ†ŒμŠ€μ½”λ“œλ‚˜ 데이터λ₯Ό 기반으둜 "μ„œλΉ„μŠ€ μ‚¬μš© μ„€λͺ… 및 μ•ˆλ‚΄, Q&Aλ₯Ό ν•˜λŠ” 역할이닀". μ•„μ£Ό μΉœμ ˆν•˜κ³  μžμ„Έν•˜κ²Œ 4000토큰 이상 Markdown ν˜•μ‹μœΌλ‘œ μž‘μ„±ν•˜λΌ. λ„ˆλŠ” μž…λ ₯된 λ‚΄μš©μ„ 기반으둜 μ‚¬μš© μ„€λͺ… 및 질의 응닡을 μ§„ν–‰ν•˜λ©°, μ΄μš©μžμ—κ²Œ 도움을 μ£Όμ–΄μ•Ό ν•œλ‹€. μ΄μš©μžκ°€ κΆκΈˆν•΄ ν•  λ§Œν•œ λ‚΄μš©μ— μΉœμ ˆν•˜κ²Œ μ•Œλ €μ£Όλ„λ‘ ν•˜λΌ. 전체 λ‚΄μš©μ— λŒ€ν•΄μ„œλŠ” λ³΄μ•ˆμ„ μœ μ§€ν•˜κ³ , ν‚€ κ°’ 및 μ—”λ“œν¬μΈνŠΈμ™€ ꡬ체적인 λͺ¨λΈμ€ κ³΅κ°œν•˜μ§€ 마라."""
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ if uploaded_file:
29
+ content = read_uploaded_file(uploaded_file)
30
+ file_extension = os.path.splitext(uploaded_file.name)[1].lower()
31
+
32
+ if file_extension == '.parquet':
33
+ system_message += f"\n\n파일 λ‚΄μš©:\n```markdown\n{content}\n```"
34
+ else:
35
+ system_message += f"\n\n파일 λ‚΄μš©:\n```python\n{content}\n```"
36
+
37
+ if message == "파일 뢄석을 μ‹œμž‘ν•©λ‹ˆλ‹€.":
38
+ message = """μ—…λ‘œλ“œλœ νŒŒμΌμ„ λΆ„μ„ν•˜μ—¬ λ‹€μŒ λ‚΄μš©μ„ ν¬ν•¨ν•˜μ—¬ μƒμ„Ένžˆ μ„€λͺ…ν•˜λΌ:
39
+ 1. 파일의 μ£Όμš” λͺ©μ κ³Ό κΈ°λŠ₯
40
+ 2. μ£Όμš” νŠΉμ§•κ³Ό κ΅¬μ„±μš”μ†Œ
41
+ 3. ν™œμš© 방법 및 μ‚¬μš© μ‹œλ‚˜λ¦¬μ˜€
42
+ 4. μ£Όμ˜μ‚¬ν•­ 및 μ œν•œμ‚¬ν•­
43
+ 5. κΈ°λŒ€νš¨κ³Ό 및 μž₯점"""
 
 
 
44
 
45
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
46
  for val in history:
 
52
 
53
  response = ""
54
  try:
55
+ for msg in hf_client.chat_completion(
56
  messages,
57
  max_tokens=max_tokens,
58
  stream=True,
59
  temperature=temperature,
60
  top_p=top_p,
61
  ):
62
+ token = msg.choices[0].delta.get('content', None)
63
  if token:
64
  response += token
65
+ return "", history + [(message, response)]
66
  except Exception as e:
67
+ error_msg = f"μΆ”λ‘  쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
68
+ return "", history + [(message, error_msg)]
69
 
70
  css = """
71
+ footer {visibility: hidden}
 
 
72
  """
73
 
 
 
 
74
  with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
75
  with gr.Row():
76
  with gr.Column(scale=2):
77
+ chatbot = gr.Chatbot(height=600, type="messages")
78
+ msg = gr.Textbox(
79
+ label="λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”",
80
+ show_label=False,
81
+ placeholder="λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”...",
82
+ container=False
83
+ )
84
  clear = gr.ClearButton([msg, chatbot])
85
 
86
  with gr.Column(scale=1):
87
+ file_upload = gr.File(
88
+ label="파일 μ—…λ‘œλ“œ (.cod, .txt, .py, .parquet)",
89
+ file_types=[".cod", ".txt", ".py", ".parquet"],
90
+ type="file"
91
+ )
 
 
 
 
 
 
 
92
 
93
  with gr.Accordion("κ³ κΈ‰ μ„€μ •", open=False):
94
  system_message = gr.Textbox(label="System Message", value="")
 
96
  temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
97
  top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  # 이벀트 바인딩
100
+ msg.submit(
101
+ chat,
102
+ inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
103
+ outputs=[msg, chatbot]
104
+ )
105
+
106
+ # 파일 μ—…λ‘œλ“œ μ‹œ μžλ™ 뢄석
107
+ file_upload.change(
108
+ chat,
109
+ inputs=[gr.Textbox(value="파일 뢄석을 μ‹œμž‘ν•©λ‹ˆλ‹€."), chatbot, file_upload, system_message, max_tokens, temperature, top_p],
110
+ outputs=[msg, chatbot]
111
+ )
112
 
113
  # 예제 μΆ”κ°€
114
  gr.Examples(