Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
-
import
|
5 |
from typing import List, Tuple
|
6 |
|
7 |
# 추론 API 클라이언트 설정
|
8 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
|
9 |
-
#hf_client = InferenceClient("CohereForAI/aya-23-35B", token=os.getenv("HF_TOKEN"))
|
10 |
|
11 |
def load_code(filename):
|
12 |
try:
|
@@ -17,21 +17,35 @@ def load_code(filename):
|
|
17 |
except Exception as e:
|
18 |
return f"파일을 읽는 중 오류가 발생했습니다: {str(e)}"
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
fashion_code = load_code('fashion.cod')
|
21 |
uhdimage_code = load_code('uhdimage.cod')
|
22 |
MixGEN_code = load_code('mgen.cod')
|
23 |
|
|
|
|
|
|
|
24 |
def respond(
|
25 |
message,
|
26 |
history: List[Tuple[str, str]],
|
27 |
system_message="", # 기본값 추가
|
28 |
-
max_tokens=1024,
|
29 |
-
temperature=0.7,
|
30 |
-
top_p=0.9,
|
31 |
):
|
32 |
-
global fashion_code, uhdimage_code, MixGEN_code
|
33 |
system_message = system_message or ""
|
34 |
-
system_prefix = """반드시 한글로 답변할것. 너는 주어진 소스코드를 기반으로 \"서비스 사용 설명 및 안내,
|
35 |
|
36 |
if message.lower() == "패션 코드 실행":
|
37 |
system_message += f"\n\n패션 코드 내용:\n{fashion_code}"
|
@@ -42,6 +56,9 @@ def respond(
|
|
42 |
elif message.lower() == "mixgen 코드 실행":
|
43 |
system_message += f"\n\nMixGEN 코드 내용:\n{MixGEN_code}"
|
44 |
message = "MixGEN3 이미지 생성에 대한 내용을 학습하였고, 설명할 준비가 되어있다고 알리고 서비스 URL(https://openfree-mixgen3.hf.space)을 통해 테스트 해보라고 출력하라."
|
|
|
|
|
|
|
45 |
|
46 |
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
47 |
for val in history:
|
@@ -52,19 +69,20 @@ def respond(
|
|
52 |
messages.append({"role": "user", "content": message})
|
53 |
|
54 |
response = ""
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
68 |
|
69 |
# Gradio 인터페이스 설정 부분도 수정
|
70 |
demo = gr.ChatInterface(
|
@@ -78,18 +96,18 @@ demo = gr.ChatInterface(
|
|
78 |
examples=[
|
79 |
["패션 코드 실행"],
|
80 |
["UHD 이미지 코드 실행"],
|
81 |
-
["MixGEN 코드 실행"],
|
|
|
82 |
["상세한 사용 방법을 마치 화면을 보면서 설명하듯이 4000 토큰 이상 자세히 설명하라"],
|
83 |
-
["FAQ 20건을 상세하게 작성하라. 4000토큰 이상 사용하라."],
|
84 |
["사용 방법과 차별점, 특징, 강점을 중심으로 4000 토큰 이상 유튜브 영상 스크립트 형태로 작성하라"],
|
85 |
["본 서비스를 SEO 최적화하여 블로그 포스트(배경 및 필요성, 기존 유사 서비스와 비교하여 특장점, 활용처, 가치, 기대효과, 결론을 포함)로 4000 토큰 이상 작성하라"],
|
86 |
-
["특허 출원에 활용할 기술 및 비즈니스모델 측면을 포함하여 특허 출원서 구성에 맞게 혁신적인 창의 발명 내용을 중심으로 4000토큰 이상 작성하라."],
|
87 |
["계속 이어서 답변하라"],
|
88 |
],
|
89 |
-
|
90 |
-
theme="Nymbo/Nymbo_Theme",
|
91 |
cache_examples=False, # 캐싱 비활성화 설정
|
92 |
)
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
+
import pandas as pd
|
5 |
from typing import List, Tuple
|
6 |
|
7 |
# 추론 API 클라이언트 설정
|
8 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
|
9 |
+
# hf_client = InferenceClient("CohereForAI/aya-23-35B", token=os.getenv("HF_TOKEN"))
|
10 |
|
11 |
def load_code(filename):
|
12 |
try:
|
|
|
17 |
except Exception as e:
|
18 |
return f"파일을 읽는 중 오류가 발생했습니다: {str(e)}"
|
19 |
|
20 |
+
def load_parquet(filename):
|
21 |
+
try:
|
22 |
+
df = pd.read_parquet(filename, engine='pyarrow')
|
23 |
+
# 데이터프레임의 첫 몇 행을 문자열로 변환
|
24 |
+
return df.head(10).to_string(index=False)
|
25 |
+
except FileNotFoundError:
|
26 |
+
return f"{filename} 파일을 찾을 수 없습니다."
|
27 |
+
except Exception as e:
|
28 |
+
return f"파일을 읽는 중 오류가 발생했습니다: {str(e)}"
|
29 |
+
|
30 |
+
# 코드 파일 로드
|
31 |
fashion_code = load_code('fashion.cod')
|
32 |
uhdimage_code = load_code('uhdimage.cod')
|
33 |
MixGEN_code = load_code('mgen.cod')
|
34 |
|
35 |
+
# Parquet 파일 로드
|
36 |
+
test_parquet_content = load_parquet('test.parquet')
|
37 |
+
|
38 |
def respond(
|
39 |
message,
|
40 |
history: List[Tuple[str, str]],
|
41 |
system_message="", # 기본값 추가
|
42 |
+
max_tokens=1024, # 기본값 추가
|
43 |
+
temperature=0.7, # 기본값 추가
|
44 |
+
top_p=0.9, # 기본값 추가
|
45 |
):
|
46 |
+
global fashion_code, uhdimage_code, MixGEN_code, test_parquet_content
|
47 |
system_message = system_message or ""
|
48 |
+
system_prefix = """반드시 한글로 답변할것. 너는 주어진 소스코드를 기반으로 \"서비스 사용 설명 및 안내, Q&A를 하는 역할이다\". 아주 친절하고 자세하게 4000토큰 이상 작성하라. 너는 코드를 기반으로 사용 설명 및 질의 응답을 진행하며, 이용자에게 도움을 주어야 한다. 이용자가 궁금해 할 만 한 내용에 친절하게 알려주도록 하라. 코드 전체 내용에 대해서는 보안을 유지하고, 키 값 및 엔드포인트와 구체적인 모델은 공개하지 마라."""
|
49 |
|
50 |
if message.lower() == "패션 코드 실행":
|
51 |
system_message += f"\n\n패션 코드 내용:\n{fashion_code}"
|
|
|
56 |
elif message.lower() == "mixgen 코드 실행":
|
57 |
system_message += f"\n\nMixGEN 코드 내용:\n{MixGEN_code}"
|
58 |
message = "MixGEN3 이미지 생성에 대한 내용을 학습하였고, 설명할 준비가 되어있다고 알리고 서비스 URL(https://openfree-mixgen3.hf.space)을 통해 테스트 해보라고 출력하라."
|
59 |
+
elif message.lower() == "test.parquet 실행":
|
60 |
+
system_message += f"\n\ntest.parquet 파일 내용:\n{test_parquet_content}"
|
61 |
+
message = "test.parquet 파일에 대한 내용을 학습하였고, 관련 설명 및 Q&A를 진행할 준비가 되어있다. 궁금한 점이 있으면 물어보라."
|
62 |
|
63 |
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
64 |
for val in history:
|
|
|
69 |
messages.append({"role": "user", "content": message})
|
70 |
|
71 |
response = ""
|
72 |
+
try:
|
73 |
+
for message in hf_client.chat_completion(
|
74 |
+
messages,
|
75 |
+
max_tokens=max_tokens,
|
76 |
+
stream=True,
|
77 |
+
temperature=temperature,
|
78 |
+
top_p=top_p,
|
79 |
+
):
|
80 |
+
token = message.choices[0].delta.get('content', None)
|
81 |
+
if token:
|
82 |
+
response += token.strip()
|
83 |
+
yield response
|
84 |
+
except Exception as e:
|
85 |
+
yield f"추론 중 오류가 발생했습니다: {str(e)}"
|
86 |
|
87 |
# Gradio 인터페이스 설정 부분도 수정
|
88 |
demo = gr.ChatInterface(
|
|
|
96 |
examples=[
|
97 |
["패션 코드 실행"],
|
98 |
["UHD 이미지 코드 실행"],
|
99 |
+
["MixGEN 코드 실행"],
|
100 |
+
["test.parquet 실행"], # 새로운 예제 추가
|
101 |
["상세한 사용 방법을 마치 화면을 보면서 설명하듯이 4000 토큰 이상 자세히 설명하라"],
|
102 |
+
["FAQ 20건을 상세하게 작성하라. 4000토큰 이상 사용하라."],
|
103 |
["사용 방법과 차별점, 특징, 강점을 중심으로 4000 토큰 이상 유튜브 영상 스크립트 형태로 작성하라"],
|
104 |
["본 서비스를 SEO 최적화하여 블로그 포스트(배경 및 필요성, 기존 유사 서비스와 비교하여 특장점, 활용처, 가치, 기대효과, 결론을 포함)로 4000 토큰 이상 작성하라"],
|
105 |
+
["특허 출원에 활용할 기술 및 비즈니스모델 측면을 포함하여 특허 출원서 구성에 맞게 혁신적인 창의 발명 내용을 중심으로 4000 토큰 이상 작성하라."],
|
106 |
["계속 이어서 답변하라"],
|
107 |
],
|
108 |
+
theme="Nymbo/Nymbo_Theme",
|
|
|
109 |
cache_examples=False, # 캐싱 비활성화 설정
|
110 |
)
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
+
demo.launch()
|