Spaces:
Paused
Paused
| import gradio as gr | |
| from gradio_client import Client | |
| import json | |
| import logging | |
| import openai | |
| import os | |
| # λ‘κΉ μ€μ | |
| 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 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", "μ λͺ© μμ") | |
| transcription_text = data.get("transcriptionAsText", "") | |
| if not transcription_text: | |
| raise ValueError("μΆμΆλ μ€ν¬λ¦½νΈκ° μμ΅λλ€.") | |
| logging.info("μ€ν¬λ¦½νΈ μΆμΆ μλ£") | |
| return title, 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(text): | |
| prompt = f""" | |
| 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 display_content(title, script, summary): | |
| return f""" | |
| <div style="display: flex; flex-direction: column; gap: 20px;"> | |
| <div style="flex: 1;"> | |
| <h3>μμ½</h3> | |
| <div style="white-space: pre-wrap; background-color: #f0f0f0; padding: 15px; border-radius: 5px;">{summary}</div> | |
| </div> | |
| <div style="flex: 1;"> | |
| <details> | |
| <summary><h3>μλ¬Έ μ€ν¬λ¦½νΈ (ν΄λ¦νμ¬ νΌμΉκΈ°)</h3></summary> | |
| <div style="white-space: pre-wrap; background-color: #e0e0e0; padding: 15px; border-radius: 5px;">{script}</div> | |
| </details> | |
| </div> | |
| <h2 style='font-size:24px;'>μμ μ λͺ©: {title}</h2> | |
| </div> | |
| """ | |
| def analyze(url, cache): | |
| try: | |
| if url == cache["url"]: | |
| logging.info(f"μΊμλ λ°μ΄ν° μ¬μ©: URL = {url}") | |
| title, script = cache["title"], cache["script"] | |
| else: | |
| logging.info(f"μλ‘μ΄ λ°μ΄ν° μΆμΆ μμ: URL = {url}") | |
| title, script = get_youtube_script(url) | |
| cache = {"url": url, "title": title, "script": script} | |
| summary = summarize_text(script) | |
| content_html = display_content(title, script, summary) | |
| logging.info("λΆμ μλ£") | |
| return content_html, cache | |
| except Exception as e: | |
| error_msg = f"μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}" | |
| logging.exception(error_msg) | |
| return error_msg, cache | |
| # Gradio μΈν°νμ΄μ€ | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## YouTube μ€ν¬λ¦½νΈ μΆμΆ λ° μμ½ λꡬ") | |
| youtube_url_input = gr.Textbox(label="YouTube URL μ λ ₯") | |
| analyze_button = gr.Button("λΆμνκΈ°") | |
| content_output = gr.HTML(label="λ΄μ©") | |
| cached_data = gr.State({"url": "", "title": "", "script": ""}) | |
| analyze_button.click( | |
| analyze, | |
| inputs=[youtube_url_input, cached_data], | |
| outputs=[content_output, cached_data] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=True) |