Spaces:
Paused
Paused
| import gradio as gr | |
| from gradio_client import Client | |
| import json | |
| import logging | |
| import openai | |
| import os | |
| import re | |
| # λ‘κΉ μ€μ | |
| logging.basicConfig(filename='youtube_script_extractor.log', level=logging.DEBUG, | |
| format='%(asctime)s - %(levelname)s - %(message)s') | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| # λ¬Έμ₯ κ΅¬λΆ ν¨μ | |
| def split_sentences(text): | |
| sentences = re.split(r"(λλ€|μμ|ꡬλ|ν΄μ|κ΅°μ|κ² μ΄μ|μμ€|ν΄λΌ|μμ|μμ|λ°μ|λμ|μΈμ|μ΄μ|κ²μ|ꡬμ|κ³ μ|λμ|νμ£ )(?![\w])", text) | |
| combined_sentences = [] | |
| current_sentence = "" | |
| for i in range(0, len(sentences), 2): | |
| if i + 1 < len(sentences): | |
| sentence = sentences[i] + sentences[i + 1] | |
| else: | |
| sentence = sentences[i] | |
| if len(current_sentence) + len(sentence) > 100: # 100μλ₯Ό μ΄κ³Όν κ²½μ° | |
| combined_sentences.append(current_sentence.strip()) | |
| current_sentence = sentence.strip() | |
| else: | |
| current_sentence += sentence | |
| if sentence.endswith(('.', '?', '!')): | |
| combined_sentences.append(current_sentence.strip()) | |
| current_sentence = "" | |
| if current_sentence: | |
| combined_sentences.append(current_sentence.strip()) | |
| return combined_sentences | |
| def parse_api_response(response): | |
| try: | |
| if isinstance(response, str): | |
| response = json.loads(response) | |
| if isinstance(response, list) and len(response) > 0: | |
| response = response[0] | |
| if not isinstance(response, dict): | |
| raise ValueError(f"μμμΉ λͺ»ν μλ΅ νμμ λλ€. λ°μ λ°μ΄ν° νμ : {type(response)}") | |
| return response | |
| except Exception as e: | |
| logging.error(f"API μλ΅ νμ± μ€ν¨: {str(e)}") | |
| raise ValueError(f"API μλ΅ νμ± μ€ν¨: {str(e)}") | |
| def get_youtube_script(url): | |
| logging.info(f"μ€ν¬λ¦½νΈ μΆμΆ μμ: URL = {url}") | |
| client = Client("whispersound/YT_Ts_R") | |
| try: | |
| result = client.predict(youtube_url=url, api_name="/predict") | |
| parsed_result = parse_api_response(result) | |
| if 'data' not in parsed_result or not parsed_result['data']: | |
| raise ValueError("API μλ΅μ μ ν¨ν λ°μ΄ν°κ° μμ΅λλ€.") | |
| data = parsed_result["data"][0] | |
| title = data.get("title", "μ λͺ© μμ") | |
| description = data.get("description", "μ€λͺ μμ") | |
| transcription_text = data.get("transcriptionAsText", "") | |
| if not transcription_text: | |
| raise ValueError("μΆμΆλ μ€ν¬λ¦½νΈκ° μμ΅λλ€.") | |
| logging.info("μ€ν¬λ¦½νΈ μΆμΆ μλ£") | |
| return title, description, transcription_text | |
| except Exception as e: | |
| logging.exception("μ€ν¬λ¦½νΈ μΆμΆ μ€ μ€λ₯ λ°μ") | |
| raise | |
| def call_api(prompt, max_tokens, temperature, top_p): | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o-mini", | |
| messages=[{"role": "user", "content": prompt}], | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p | |
| ) | |
| return response['choices'][0]['message']['content'] | |
| except Exception as e: | |
| logging.exception("LLM API νΈμΆ μ€ μ€λ₯ λ°μ") | |
| raise | |
| def summarize_text(title, description, text): | |
| prompt = f""" | |
| μ λͺ©: {title} | |
| μ€λͺ : {description} | |
| μμ μ λͺ©κ³Ό μ€λͺ μ μ΄ μ νλΈ μμμ μλ³Έ λ©νλ°μ΄ν°μ λλ€. μ΄λ₯Ό μ°Έκ³ νμ¬ μλμ λλ³Έμ μμ½ν΄μ£ΌμΈμ. | |
| 1. μμ μ λͺ©κ³Ό μ€λͺ μ μ°Έκ³ νμ¬ μ νλΈ λλ³Έμ ν΅μ¬ μ£Όμ μ λͺ¨λ μ£Όμ λ΄μ©μ μμΈνκ² μμ½νλΌ | |
| 2. λ°λμ νκΈλ‘ μμ±νλΌ | |
| 3. μμ½λ¬Έλ§μΌλ‘λ μμμ μ§μ μμ²ν κ²κ³Ό λμΌν μμ€μΌλ‘ λ΄μ©μ μ΄ν΄ν μ μλλ‘ μμΈν μμ± | |
| 4. κΈμ λ무 μμΆνκ±°λ ν¨μΆνμ§ λ§κ³ , μ€μν λ΄μ©κ³Ό μΈλΆμ¬νμ λͺ¨λ ν¬ν¨ | |
| 5. λ°λμ λλ³Έμ νλ¦κ³Ό λ Όλ¦¬ ꡬ쑰λ₯Ό μ μ§ | |
| 6. λ°λμ μκ° μμλ μ¬κ±΄μ μ κ° κ³Όμ μ λͺ ννκ² λ°μ | |
| 7. λ±μ₯μΈλ¬Ό, μ₯μ, μ¬κ±΄ λ± μ€μν μμλ₯Ό μ ννκ² μμ± | |
| 8. λλ³Έμμ μ λ¬νλ κ°μ μ΄λ λΆμκΈ°λ ν¬ν¨ | |
| 9. λ°λμ κΈ°μ μ μ©μ΄λ μ λ¬Έ μ©μ΄κ° μμ κ²½μ°, μ΄λ₯Ό μ ννκ² μ¬μ© | |
| 10. λλ³Έμ λͺ©μ μ΄λ μλλ₯Ό νμ νκ³ , μ΄λ₯Ό μμ½μ λ°λμ λ°μ | |
| 11. κ° λ¬Έμ₯μ λͺ ννκ² κ΅¬λΆνκ³ , μ μ ν λ¨λ½ ꡬλΆμ μ¬μ©νμ¬ κ°λ μ±μ λμ΄μμ€ | |
| λλ³Έ: | |
| {text} | |
| """ | |
| return call_api(prompt, max_tokens=2000, temperature=0.3, top_p=0.9) | |
| def analyze(url, progress=gr.Progress()): | |
| try: | |
| progress(0, desc="μ€ν¬λ¦½νΈ μΆμΆ μ€...") | |
| title, description, script = get_youtube_script(url) | |
| progress(33, desc="μλ¬Έ μ€ν¬λ¦½νΈ μ²λ¦¬ μ€...") | |
| script_sentences = split_sentences(script) | |
| script_content = "\n".join(script_sentences) | |
| progress(66, desc="μμ½ μμ± μ€...") | |
| summary = summarize_text(title, description, script) | |
| progress(100, desc="μλ£") | |
| return { | |
| "μ λͺ©": title, | |
| "μλ¬Έ μ€ν¬λ¦½νΈ": script_content, | |
| "μμ½": summary | |
| } | |
| except Exception as e: | |
| error_msg = f"μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}" | |
| logging.exception(error_msg) | |
| return {"μ€λ₯": error_msg} | |
| # Gradio μΈν°νμ΄μ€ | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## YouTube μ€ν¬λ¦½νΈ μΆμΆ λ° μμ½ λꡬ") | |
| youtube_url_input = gr.Textbox(label="YouTube URL μ λ ₯") | |
| analyze_button = gr.Button("λΆμνκΈ°") | |
| with gr.Tabs(): | |
| with gr.TabItem("μλ¬Έ μ€ν¬λ¦½νΈ"): | |
| script_output = gr.Markdown() | |
| with gr.TabItem("μμ½"): | |
| summary_output = gr.Markdown() | |
| title_output = gr.Textbox(label="μμ μ λͺ©") | |
| analyze_button.click( | |
| analyze, | |
| inputs=[youtube_url_input], | |
| outputs=[title_output, script_output, summary_output] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |