Spaces:
Build error
Build error
| from openai import OpenAI | |
| import openai | |
| import base64 | |
| import requests, json | |
| from docx import Document | |
| from docx.shared import Cm, Inches | |
| from PIL import Image | |
| import gradio as gr | |
| import tempfile | |
| import os | |
| import shutil | |
| # ์ด๋ฏธ์ง ์ฒ๋ฆฌ | |
| def encode_image(image_path): | |
| with open(image_path, "rb") as image_file: | |
| return base64.b64encode(image_file.read()).decode("utf-8") | |
| #๋ฌธ์ ์ | |
| def word(img, gpt_text,text,img_prob,voice_prob ): | |
| doc = Document() | |
| # heading์ level์ ํตํด ์กฐ์ | |
| doc.add_heading("๋ฅํ์ดํฌ ์ ๊ณ ์์ฑ", level = 0) | |
| #doc.add_heading(f"์์ ๋ถ์ผ: {crime}", level = 1) | |
| # ๋ฌธ๋จ ์ฐ๊ธฐ | |
| paragraph1 = doc.add_paragraph(f"๋์์ ๋ฅํ์ดํฌ ํ๋ฅ : {img_prob}") | |
| if img_prob > 0.5: | |
| paragraph1.add_run("\n๋ฌธ์ ๊ฐ ๋๋ ์์ ์ด๋ฏธ์ง :") | |
| doc.add_picture(img,width= Cm(6), height= Cm(3)) | |
| # ๋ฌธ๋จ ๋ด์ฉ ์ด์ด ์ฐ๊ธฐ | |
| paragraph2 = doc.add_paragraph(f"์์ฑ ๋ฅํ์ดํฌ ํ๋ฅ : {voice_prob}") | |
| for i in range(len(voice_prob)): | |
| if voice_prob[i] > 0.5: | |
| t = text[i] | |
| paragraph2.add_run(f"\n๋ฌธ์ ๊ฐ ๋๋ ํ ์คํธ ๋ด์ฉ: \n\"{t}\"") | |
| doc.add_heading("๋ฅํ์ดํฌ ์ ๊ณ ๋ด์ฉ", level = 2) | |
| paragraph3 = doc.add_paragraph(gpt_text) | |
| # ๋ฌธ์ ์ ์ฅ | |
| file_path = "sample.docx" | |
| doc.save(file_path) | |
| return file_path | |
| def gpt_prompt(voice_prob,img_prob, text1,crop_img,first_img): | |
| #ํ๋กฌํํธ | |
| message=[ | |
| # ์ด๋ป๊ฒ ํ๋์ ํ ์ง ์ง์ ํ๋ ๊ธฐ๋ฅ | |
| {"role": "system", "content": "You are an expert on deepfake criminal activity."}, | |
| # ์ง๋ฌธ ๋ด์ฉ | |
| {"role": "user", "content": f""" | |
| ์ฒซ๋ฒ์งธ๋ก ์ ๋ ฅ๋ ์ด๋ฏธ์ง๋ ์์์ ์ฒซ ํ๋ ์ ์ด๋ฏธ์ง์ผ. ๊ทธ ์ด๋ฏธ์ง์ ํ ์คํธ๋ฅผ ๋ณด๊ณ ์์์ ์ฌํฉ๊ณผ ๋ด์ฉ์ ์ค๋ช ํด์ค. ex) ์ด๋ค์ฌ๋(์ ๋ช ์ธ, ์ ์น์ธ, ์ผ๋ฐ์ธ)์ด ๋ฌด์์ ํ๋์ง(๊ด๊ณ , ์ธํฐ๋ทฐ, ๋ด์ค ๋ฑ๋ฑ) | |
| ๋๋ฒ์งธ ์ด๋ฏธ์ง๋ ์์์ ์ผ๋ถ๋ฅผ ์๋ผ์จ๊ฑฐ์ผ. ๋ฅํ์ดํฌ ์์ ํ๋ฅ : {img_prob}์ ์ฃผ์ด์ง ์ด๋ฏธ์ง์ ๋ํ ๋ฅํ์ดํฌ ํ๋ฅ ์ด์ผ. ์ด ๊ฐ์ด 0.5๋ณด๋ค ํฌ๋ฉด, ๋๋ฒ์งธ ์ด๋ฏธ์ง๋ ์กฐ์๋์๋ค๋๊ฑฐ์ผ. | |
| ์์ฑ ๋ฅํ์ดํฌ ํ๋ฅ : {voice_prob} / prob์ ๋ฆฌ์คํธ ๊ธธ์ด๋ ๋ฐํ์์ ๊ฐ์์ผ. ๊ฐ์ด ํ๋๋ฉด ํ๋ช ์ ๋ชฉ์๋ฆฌ์ด๊ณ ๋งํ๋ ์์๋๋ก ๋์ด๋๊ฑฐ์ผ. ๊ทธ ๊ฐ์ด 0.5๋ณด๋ค ๋์ผ๋ฉด ์กฐ์๋ ๋ด์ฉ์ด๋ผ๋ ๋ป์ด์ผ. | |
| ๋ฐ๋๋ก 0.5๋ณด๋ค ๋ฎ์ผ๋ฉด ์กฐ์๋์ง ์์ ์ง์ง ๋ด์ฉ์ด๋ผ๋ ์๋ฆฌ์ผ | |
| ์์ฑ ๋ด์ฉ: {text1} / ์ผํ๋ก ๊ตฌ๋ถ๋ ๊ฐ ๋ฌธ์ฅ์ ์์ prob์ index์ ๋ง๊ฒ ์์ฑ์ ํ์คํธ๋ก ์ถ์ถํ๊ฑฐ์ผ. ํ ์คํธ์ ๋ด์ฉ์ด ์กฐ์๋์์ ํ๋ฅ ์ด 0.5๋ฅผ ๋๊ธฐ๋ฉด ๊ทธ ๋ด์ฉ์ ๊ฐ์ง ๋ด์ฉ์ด์ผ. ๋ฐ๋๋ก ํ ์คํธ๊ฐ ์กฐ์ ๋์์ํ๋ฅ ์ด 0.5๋ณด๋ค ๋ฎ์ผ๋ฉด | |
| ๊ทธ ๋ด์ฉ์ ์ง์ง ๋ด์ฉ์ด์ผ. | |
| ์ฝํ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ง์ง๋ง ๋ฌธ์ฅ์ ์์์ ์ ์ฒด text์ผ. | |
| ๋ต๋ณ์ ์์ฑ๋ด์ฉ์ ์ฒซ๋ฒ์งธ ์ฝํ ๊น์ง์ ๋ฌธ์ฅ ์ฌ์ฉํด์ค. | |
| ์ฃผ์ด์ง ์ด๋ฏธ์ง, ํ ์คํธ, ๋ฅํ์ดํฌ ํ๋ฅ ์ ๊ฐ์ง๊ณ ์ ๊ณ ์ ์์ฑ์ ๋ชฉ์ ์ผ๋ก ์ ๊ณ ์ ์์ฑ ์๋ ํ์ผ์ ๋ง๋๋ ค๊ณ ํด | |
| 1.์ฑ๋ฒ์ฃ 2.๋ช ์ํผ์ 3. ํ์์์ :๊ธ์ต ๋ฐ ์ฌ๊ธฐ (๊ฐ์ง ๊ด๊ณ ) 4. ์ ์น ๋ฐ ์ ๊ฑฐ ๊ฐ์ (๊ฐ์ง ๋ด์ค, ์ ๊ฑฐ). | |
| ํด๋น ๋ฅํ์ดํฌ ์์์ text ๊ฐ ์ด๋ค ๋ฒ์ฃ ํ์์ ํด๋นํ๋์ง ํ์ธํ๊ณ ์ ๊ณ ์ ์์์ ๋ง๊ฒ ์ ๊ณ ์ ๋ด์ฉ์ ์์ฑํด์ค. | |
| ์ด๋ฏธ์ง์ ์์ฑ์ ๋ฅํ์ดํฌ ํ๋ฅ ๊ฐ์ ์ด๋ป๊ฒ ๋์์ ๋ฅํ์ดํฌ๋ก ์์ฌ๋๋์ง ์ค๋ช ํ๊ณ | |
| ์ด๋ค ๋ฒ์ฃ ํ์์ธ์ง, text์ ๋ฌธ์ ๊ฐ ์๋ค๋ฉด ์ด๋ค ๋ด์ฉ์ด๊ณ ๋ญ๊ฐ ๋ฌธ์ ์ธ์ง์ ๋ํ ๋ด์ฉ์ ์์ธํ๊ฒ ์ค๋ช ํ๊ณ ํฌํจํด์ผ ํด. | |
| ๋ฅํ์ดํฌ๋ก ํ์ ๋ ์์์ด ๊ณ์ ์ฌ์ฉ๋๋ค๋ฉด ์ด๋ค ๋ฌธ์ ๊ฐ ๋ฐ์ ํ ์ ์๋์ง๋ ์ถ๊ฐํด์ค. | |
| ์๋ก์ด ๋ฌธ์ฅ ์์๋ \n์ ์ถ๊ฐํ๊ณ ์์ํด์ค. | |
| **๋ ์ฌ์ฉํ์ง ๋ง. | |
| """}, | |
| #์ด๋ฏธ์ง ์ ๋ ฅ | |
| {"role": "user","content": [ | |
| {"type": "text","text": """ ์ฒซ๋ฒ์งธ๋ก ์ ๋ ฅ๋ ์ด๋ฏธ์ง๋ ์์์ ์ฒซ ํ๋ ์ ์ด๋ฏธ์ง์ผ. ๊ทธ ์ด๋ฏธ์ง์ ํ ์คํธ๋ฅผ ๋ณด๊ณ ์์์ ์ฌํฉ๊ณผ ๋ด์ฉ์ ์ค๋ช ํด์ค. ex) ์ด๋ค์ฌ๋(์ ๋ช ์ธ, ์ ์น์ธ, ์ผ๋ฐ์ธ)์ด ๋ฌด์์ ํ๋์ง(๊ด๊ณ , ์ธํฐ๋ทฐ, ๋ด์ค ๋ฑ๋ฑ) | |
| ๋๋ฒ์งธ ์ด๋ฏธ์ง๋ ์์์ ์ผ๋ถ๋ฅผ ์๋ผ์จ๊ฑฐ์ผ. ๋ฅํ์ดํฌ ์์ ํ๋ฅ ์ ํ์ธํด์ ๊ทธ๊ทธ ๊ฐ์ด 0.5๋ณด๋ค ํฌ๋ฉด, ๋๋ฒ์งธ ์ด๋ฏธ์ง๋ ์กฐ์๋์๋ค๋๊ฑฐ์ผ. """}, | |
| {"type": "image_url","image_url": {"url": f"data:image/jpeg;base64,{first_img}"}}, | |
| {"type": "image_url","image_url": {"url": f"data:image/jpeg;base64,{crop_img}"}} | |
| ]}, | |
| # ๋ชจ๋ธ์ด ์๋ตํ ๋ ์ฌ์ฉํ๋ ์ญํ | |
| {"role": "assistant", "content": """ | |
| ๋๊ฐ ์์ฑํด์ค ๋ด์ฉ | |
| ์ ๊ณ ์ ๋ชฉ : ์ ๊ณ ์ ํต์ฌ๋ด์ฉ | |
| ์ ๊ณ ๋ด์ฉ : ์ ๊ณ ์ด์ ๋ฐ ํ์์กฐ์์ฝํ ์ธ ๊ฐ ๋๋ฌ๋๋ ๋ถ๋ถ ๋ช ์ | |
| ์ฆ๊ฑฐ์๋ฃ : ๋ฌธ์ ๊ฐ ๋๋ ํ๋ฉด์ ์บก์ฒ ๊ทธ๋ฆผํ์ผ | |
| ๊ฐ ๋ด์ฉ๋น ์ฌ๋ฌ ๋จ๋ฝ์ผ๋ก ๋๋ ์ ๋ ผ๋ฆฌ์ ์ผ๋ก ์์ฑํด์ค์ค. | |
| ๋ต๋ณ์ **๋ค์ ์ญ์ ํด์ค | |
| """} | |
| ] | |
| return message | |
| def main(first_img,image, img_prob, voice_prob, voice_text): | |
| GPT_api_key = os.getenv("GPT_api_key") | |
| client = OpenAI(api_key=GPT_api_key) | |
| # Hugging Face Secrets์์ API ํค ๊ฐ์ ธ์ค๊ธฐ | |
| # openai.api_key = os.getenv("OPENAI_API_KEY") | |
| model="gpt-4o-mini" | |
| # if os.path.exists('/tmp/wav'): | |
| # shutil.rmtree('/tmp/wav') | |
| # #os.listdir('/tmp/wav') | |
| first_img_path = first_img | |
| image_path = image | |
| img = Image.open(image_path) | |
| img.save(image_path,'JPEG') | |
| image_path = image | |
| #image.save(image_path) | |
| #base64_image = encode_image(image_path) | |
| first_img = encode_image(first_img_path) | |
| image = encode_image(image_path) | |
| img_prob = eval(img_prob) | |
| voice_prob = eval(voice_prob) | |
| voice_text = eval(voice_text) | |
| #ํ๋กฌํํธ | |
| message = gpt_prompt(voice_prob,img_prob, voice_text,image,first_img) | |
| response = client.chat.completions.create( | |
| model=model, | |
| messages=message, | |
| ) | |
| # response = openai.ChatCompletion.create( | |
| # model=model, | |
| # messages=message | |
| # ) | |
| gpt_text = response.choices[0].message.content | |
| #print(gpt_text) | |
| with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_image_file: | |
| temp_image_path = temp_image_file.name | |
| with open(temp_image_path, 'wb') as f: | |
| f.write(base64.b64decode(image)) | |
| doc = word(temp_image_path, gpt_text, voice_text, img_prob,voice_prob) | |
| return doc, gpt_text | |
| # Gradio ์ธํฐํ์ด์ค ์์ฑ | |
| iface = gr.Interface( | |
| fn=main, | |
| inputs=[ | |
| gr.Image(label="์ฒซ ํ๋ ์ ์ด๋ฏธ์ง ์ ๋ก๋", type="filepath"), # ์ด๋ฏธ์ง ์ ๋ ฅ | |
| gr.Image(label="๋ฅํ์ดํฌ ์ด๋ฏธ์ง ์ ๋ก๋", type="filepath"), # ์ด๋ฏธ์ง ์ ๋ ฅ | |
| gr.Textbox(label="์ด๋ฏธ์ง ํ๋ฅ ๊ฐ ์ ๋ ฅ (์: 0.74 )"), # ์ด๋ฏธ์ง ํ๋ฅ ๊ฐ | |
| gr.Textbox(label="์์ฑ ํ๋ฅ ๊ฐ ์ ๋ ฅ (์: [0.90] )"), # ์์ฑ ํ๋ฅ ๊ฐ | |
| gr.Textbox(label="ํ ์คํธ ์ ๋ ฅ (์: ['๋ฌธ์ฅ1', '๋ฌธ์ฅ2'] )"), # ํ ์คํธ ์ ๋ ฅ | |
| ], | |
| outputs=[ | |
| gr.File(label="๋ค์ด๋ก๋ํ ์ ๊ณ ์ ํ์ผ"), # ์ ๊ณ ์ ํ์ผ | |
| gr.Textbox(label="GPT_๋ด์ฉ") | |
| ], | |
| title="๋ฅํ์ดํฌ ์ ๊ณ ์ ์์ฑ๊ธฐ", | |
| ) | |
| # Gradio ์ธํฐํ์ด์ค ์คํ | |
| if __name__ == "__main__": | |
| iface.launch() |