ginipick commited on
Commit
bb4b5d3
·
verified ·
1 Parent(s): e67418b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -26
app.py CHANGED
@@ -910,27 +910,33 @@ def respond(
910
  top_p,
911
  ):
912
  if not url.startswith('http'):
913
- return "Please enter a valid URL"
914
-
915
- # 기사 내용 추출
916
- article_content = get_article_content(url)
917
-
918
- # 번역 요청을 위한 프롬프트 구성
919
- translation_prompt = f"""Please translate the following article to Korean.
920
- Maintain the original meaning and context while making it natural in Korean.
921
-
922
- Article: {article_content}
923
-
924
- Korean translation:"""
925
-
926
- messages = [
927
- {"role": "system", "content": system_message or "You are a helpful translator that translates articles to Korean."},
928
- {"role": "user", "content": translation_prompt}
929
- ]
930
 
931
- response = ""
932
-
933
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
934
  for message in client.chat.completions.create(
935
  model="CohereForAI/c4ai-command-r-plus-08-2024",
936
  max_tokens=max_tokens,
@@ -939,15 +945,23 @@ def respond(
939
  top_p=top_p,
940
  messages=messages,
941
  ):
942
- token = message.choices[0].delta.content
943
- if token:
944
- response += token
945
- yield response
 
 
 
 
946
  except Exception as e:
947
- yield f"Translation error: {str(e)}"
 
 
 
 
948
 
949
 
950
- with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI 서비스") as iface:
951
  with gr.Tabs():
952
  # 국가별 탭
953
  with gr.Tab("국가별"):
@@ -1065,7 +1079,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI 서비스") as
1065
  label="Top-P"
1066
  )
1067
 
1068
- translate_button = gr.Button("번역", variant="primary")
1069
 
1070
  # 이벤트 연결
1071
  translate_button.click(
 
910
  top_p,
911
  ):
912
  if not url.startswith('http'):
913
+ history.append((url, "올바른 URL을 입력해주세요."))
914
+ return history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
915
 
 
 
916
  try:
917
+ # 기사 내용 추출
918
+ article_content = get_article_content(url)
919
+
920
+ # 번역 요청을 위한 프롬프트 구성
921
+ translation_prompt = f"""Please translate the following article to Korean.
922
+ Maintain the original meaning and context while making it natural in Korean.
923
+
924
+ Article: {article_content}
925
+
926
+ Korean translation:"""
927
+
928
+ messages = [
929
+ {"role": "system", "content": system_message or "You are a helpful translator that translates articles to Korean."},
930
+ {"role": "user", "content": translation_prompt}
931
+ ]
932
+
933
+ # 사용자 입력을 히스토리에 추가
934
+ history.append((url, "번역을 시작합니다..."))
935
+
936
+ # 번역 결과를 저장할 변수
937
+ full_response = ""
938
+
939
+ # 스트리밍 응답 처리
940
  for message in client.chat.completions.create(
941
  model="CohereForAI/c4ai-command-r-plus-08-2024",
942
  max_tokens=max_tokens,
 
945
  top_p=top_p,
946
  messages=messages,
947
  ):
948
+ if hasattr(message.choices[0].delta, 'content'):
949
+ token = message.choices[0].delta.content
950
+ if token:
951
+ full_response += token
952
+ # 마지막 메시지를 업데이트
953
+ history[-1] = (url, full_response)
954
+ yield history
955
+
956
  except Exception as e:
957
+ error_message = f"번역 오류가 발생했습니다: {str(e)}"
958
+ history.append((url, error_message))
959
+ yield history
960
+
961
+ return history
962
 
963
 
964
+ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="NewsAI 서비스") as iface:
965
  with gr.Tabs():
966
  # 국가별 탭
967
  with gr.Tab("국가별"):
 
1079
  label="Top-P"
1080
  )
1081
 
1082
+ translate_button = gr.Button("기사 생성", variant="primary")
1083
 
1084
  # 이벤트 연결
1085
  translate_button.click(