aliceblue11 commited on
Commit
790f295
·
verified ·
1 Parent(s): 62497de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -67
app.py CHANGED
@@ -1,68 +1,28 @@
1
  import gradio as gr
2
- import random
3
- import os
4
- import pandas as pd # 엑셀 파일 처리를 위한 pandas 라이브러리 추가
5
- from huggingface_hub import InferenceClient
6
-
7
- MODELS = {
8
- "Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
9
- "DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct",
10
- "Meta Llama 3.1 8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
11
- "Meta-Llama 3.1 70B-Instruct": "meta-llama/Meta-Llama-3.1-70B-Instruct",
12
- "Microsoft": "microsoft/Phi-3-mini-4k-instruct",
13
- "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3",
14
- "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
15
- "Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
16
- "Aya-23-35B": "CohereForAI/aya-23-35B"
17
- }
18
-
19
- def create_client(model_name):
20
- return InferenceClient(model_name, token=os.getenv("HF_TOKEN"))
21
-
22
- def call_api(model, content, system_message, max_tokens, temperature, top_p):
23
- client = create_client(MODELS[model])
24
- messages = [{"role": "system", "content": system_message}, {"role": "user", "content": content}]
25
- random_seed = random.randint(0, 1000000)
26
- response = client.chat_completion(messages=messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p, seed=random_seed)
27
- return response.choices[0].message.content
28
-
29
- def generate_text(model, user_message, system_message, max_tokens, temperature, top_p):
30
- return call_api(model, user_message, system_message, max_tokens, temperature, top_p)
31
-
32
- # 엑셀 파일을 처리하는 함수 추가
33
- def process_excel(file):
34
- if file is not None:
35
- df = pd.read_excel(file.name) # 엑셀 파일을 읽음
36
- return df.head().to_string() # 엑셀 파일의 처음 몇 줄을 출력
37
- else:
38
- return "엑셀 파일이 업로드되지 않았습니다."
39
-
40
- title = "AI 텍스트 생성기"
41
-
42
- with gr.Blocks() as demo:
43
- gr.Markdown(f"# {title}")
44
-
45
- model = gr.Radio(choices=list(MODELS.keys()), label="언어 모델 선택", value="Zephyr 7B Beta")
46
- user_message = gr.Textbox(label="사용자 메시지", lines=5)
47
- system_message = gr.Textbox(label="시스템 메시지 (프롬프트)", lines=10)
48
-
49
- with gr.Accordion("고급 설정", open=False):
50
- max_tokens = gr.Slider(label="Max Tokens", minimum=0, maximum=4000, value=500, step=100)
51
- temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, value=0.75, step=0.05)
52
- top_p = gr.Slider(label="Top P", minimum=0.1, maximum=1.0, value=0.95, step=0.05)
53
-
54
- # 엑셀 파일 업로드 컴포넌트 추가
55
- excel_file = gr.File(label="엑셀 파일 업로드", type="file")
56
- excel_output = gr.Textbox(label="엑셀 내용", lines=10)
57
-
58
- generate_btn = gr.Button("텍스트 생성하기")
59
- output = gr.Textbox(label="생성된 텍스트", lines=10)
60
-
61
- generate_btn.click(fn=generate_text,
62
- inputs=[model, user_message, system_message, max_tokens, temperature, top_p],
63
- outputs=[output])
64
-
65
- # 엑셀 파일 업로드 버튼과 처리 함수 연결
66
- excel_file.upload(fn=process_excel, inputs=[excel_file], outputs=[excel_output])
67
-
68
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer
3
+
4
+ # 모델과 토크나이저 로드
5
+ model_name = "Helsinki-NLP/opus-mt-ko-en" # 한국어 to 영어 모델
6
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
7
+ model = MarianMTModel.from_pretrained(model_name)
8
+
9
+ # 번역 함수 정의
10
+ def translate(text):
11
+ # 텍스트를 토큰화하고 모델에 입력
12
+ tokenized_text = tokenizer.prepare_seq2seq_batch([text], return_tensors="pt")
13
+ translated = model.generate(**tokenized_text)
14
+ # 번역된 텍스트를 디코딩
15
+ translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
16
+ return translated_text
17
+
18
+ # Gradio 인터페이스 설정
19
+ iface = gr.Interface(
20
+ fn=translate,
21
+ inputs=gr.inputs.Textbox(lines=2, placeholder="번역할 텍스트를 입력하세요..."),
22
+ outputs="text",
23
+ title="한국어 to 영어 번역기",
24
+ description="한국어 텍스트를 영어로 번역해주는 간단한 번역기입니다."
25
+ )
26
+
27
+ # 앱 실행
28
+ iface.launch()