Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -1,5 +1,6 @@ 
     | 
|
| 1 | 
         
             
            import gradio as gr
         
     | 
| 2 | 
         
             
            from huggingface_hub import InferenceClient
         
     | 
| 
         | 
|
| 3 | 
         
             
            import os
         
     | 
| 4 | 
         
             
            import random
         
     | 
| 5 | 
         
             
            import logging
         
     | 
| 
         @@ -18,7 +19,8 @@ MODELS = { 
     | 
|
| 18 | 
         
             
                "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3",
         
     | 
| 19 | 
         
             
                "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
         
     | 
| 20 | 
         
             
                "Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
         
     | 
| 21 | 
         
            -
                "Aya-23-35B": "CohereForAI/aya-23-35B"
         
     | 
| 
         | 
|
| 22 | 
         
             
            }
         
     | 
| 23 | 
         | 
| 24 | 
         
             
            # HuggingFace 토큰 설정
         
     | 
| 
         @@ -26,7 +28,13 @@ hf_token = os.getenv("HF_TOKEN") 
     | 
|
| 26 | 
         
             
            if not hf_token:
         
     | 
| 27 | 
         
             
                raise ValueError("HF_TOKEN 환경 변수가 설정되지 않았습니다.")
         
     | 
| 28 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 29 | 
         
             
            def call_hf_api(prompt, reference_text, max_tokens, temperature, top_p, model):
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 30 | 
         
             
                client = InferenceClient(model=model, token=hf_token)
         
     | 
| 31 | 
         
             
                combined_prompt = f"{prompt}\n\n참고 텍스트:\n{reference_text}"
         
     | 
| 32 | 
         
             
                random_seed = random.randint(0, 1000000)
         
     | 
| 
         @@ -44,8 +52,26 @@ def call_hf_api(prompt, reference_text, max_tokens, temperature, top_p, model): 
     | 
|
| 44 | 
         
             
                    logging.error(f"HuggingFace API 호출 중 오류 발생: {str(e)}")
         
     | 
| 45 | 
         
             
                    return f"응답 생성 중 오류 발생: {str(e)}. 나중에 다시 시도해 주세요."
         
     | 
| 46 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 47 | 
         
             
            def generate_response(prompt, reference_text, max_tokens, temperature, top_p, model):
         
     | 
| 48 | 
         
            -
                 
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 49 | 
         
             
                response_html = f"""
         
     | 
| 50 | 
         
             
                <h3>생성된 응답:</h3>
         
     | 
| 51 | 
         
             
                <div style='max-height: 500px; overflow-y: auto; white-space: pre-wrap; word-wrap: break-word;'>
         
     | 
| 
         @@ -79,4 +105,4 @@ with gr.Blocks() as demo: 
     | 
|
| 79 | 
         
             
                )
         
     | 
| 80 | 
         | 
| 81 | 
         
             
            # 인터페이스 실행
         
     | 
| 82 | 
         
            -
            demo.launch(share=True)
         
     | 
| 
         | 
|
| 1 | 
         
             
            import gradio as gr
         
     | 
| 2 | 
         
             
            from huggingface_hub import InferenceClient
         
     | 
| 3 | 
         
            +
            import openai  # OpenAI API를 사용하기 위해 추가
         
     | 
| 4 | 
         
             
            import os
         
     | 
| 5 | 
         
             
            import random
         
     | 
| 6 | 
         
             
            import logging
         
     | 
| 
         | 
|
| 19 | 
         
             
                "Mixtral 8x7B": "mistralai/Mistral-7B-Instruct-v0.3",
         
     | 
| 20 | 
         
             
                "Mixtral Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
         
     | 
| 21 | 
         
             
                "Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
         
     | 
| 22 | 
         
            +
                "Aya-23-35B": "CohereForAI/aya-23-35B",
         
     | 
| 23 | 
         
            +
                "GPT-4o Mini": "gpt-4o-mini"  # 새로운 모델 추가
         
     | 
| 24 | 
         
             
            }
         
     | 
| 25 | 
         | 
| 26 | 
         
             
            # HuggingFace 토큰 설정
         
     | 
| 
         | 
|
| 28 | 
         
             
            if not hf_token:
         
     | 
| 29 | 
         
             
                raise ValueError("HF_TOKEN 환경 변수가 설정되지 않았습니다.")
         
     | 
| 30 | 
         | 
| 31 | 
         
            +
            # OpenAI API 키 설정
         
     | 
| 32 | 
         
            +
            openai.api_key = os.getenv("OPENAI_API_KEY")
         
     | 
| 33 | 
         
            +
             
     | 
| 34 | 
         
             
            def call_hf_api(prompt, reference_text, max_tokens, temperature, top_p, model):
         
     | 
| 35 | 
         
            +
                if model == "gpt-4o-mini":
         
     | 
| 36 | 
         
            +
                    return call_openai_api(prompt, reference_text, max_tokens, temperature, top_p)
         
     | 
| 37 | 
         
            +
                
         
     | 
| 38 | 
         
             
                client = InferenceClient(model=model, token=hf_token)
         
     | 
| 39 | 
         
             
                combined_prompt = f"{prompt}\n\n참고 텍스트:\n{reference_text}"
         
     | 
| 40 | 
         
             
                random_seed = random.randint(0, 1000000)
         
     | 
| 
         | 
|
| 52 | 
         
             
                    logging.error(f"HuggingFace API 호출 중 오류 발생: {str(e)}")
         
     | 
| 53 | 
         
             
                    return f"응답 생성 중 오류 발생: {str(e)}. 나중에 다시 시도해 주세요."
         
     | 
| 54 | 
         | 
| 55 | 
         
            +
            def call_openai_api(content, system_message, max_tokens, temperature, top_p):
         
     | 
| 56 | 
         
            +
                response = openai.ChatCompletion.create(
         
     | 
| 57 | 
         
            +
                    model="gpt-4o-mini",  # 모델 ID
         
     | 
| 58 | 
         
            +
                    messages=[
         
     | 
| 59 | 
         
            +
                        {"role": "system", "content": system_message},
         
     | 
| 60 | 
         
            +
                        {"role": "user", "content": content},
         
     | 
| 61 | 
         
            +
                    ],
         
     | 
| 62 | 
         
            +
                    max_tokens=max_tokens,
         
     | 
| 63 | 
         
            +
                    temperature=temperature,
         
     | 
| 64 | 
         
            +
                    top_p=top_p,
         
     | 
| 65 | 
         
            +
                )
         
     | 
| 66 | 
         
            +
                return response.choices[0].message['content']
         
     | 
| 67 | 
         
            +
             
     | 
| 68 | 
         
             
            def generate_response(prompt, reference_text, max_tokens, temperature, top_p, model):
         
     | 
| 69 | 
         
            +
                if model == "GPT-4o Mini":
         
     | 
| 70 | 
         
            +
                    system_message = "이것은 사용자 요청에 대한 참고 텍스트를 활용하여 응답을 생성하는 작업입니다."
         
     | 
| 71 | 
         
            +
                    response = call_openai_api(prompt, reference_text, max_tokens, temperature, top_p)
         
     | 
| 72 | 
         
            +
                else:
         
     | 
| 73 | 
         
            +
                    response = call_hf_api(prompt, reference_text, max_tokens, temperature, top_p, MODELS[model])
         
     | 
| 74 | 
         
            +
                
         
     | 
| 75 | 
         
             
                response_html = f"""
         
     | 
| 76 | 
         
             
                <h3>생성된 응답:</h3>
         
     | 
| 77 | 
         
             
                <div style='max-height: 500px; overflow-y: auto; white-space: pre-wrap; word-wrap: break-word;'>
         
     | 
| 
         | 
|
| 105 | 
         
             
                )
         
     | 
| 106 | 
         | 
| 107 | 
         
             
            # 인터페이스 실행
         
     | 
| 108 | 
         
            +
            demo.launch(share=True)
         
     |