Spaces:
Runtime error
Runtime error
initial commit
Browse files
app.py
CHANGED
|
@@ -1,63 +1,73 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 7 |
+
from langchain_community.vectorstores import FAISS
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
MODEL = "gpt-4o"
|
| 11 |
+
API_KEY = "sk-proj-FV9lzQDevcA7M7yllkL7T3BlbkFJgjk8JBewp08UwSFJwaXD"
|
| 12 |
+
# BASE_URL = "https://youtu.be/"
|
| 13 |
+
|
| 14 |
+
client = OpenAI(api_key = API_KEY)
|
| 15 |
+
embeddings = OpenAIEmbeddings(model = "text-embedding-3-large", api_key = API_KEY)
|
| 16 |
+
yt_chunks = FAISS.load_local("vector-large", embeddings, allow_dangerous_deserialization = True)
|
| 17 |
+
df = pd.read_csv("../data/ko-youtube-trans-U10k.csv")
|
| 18 |
+
|
| 19 |
+
def find_docs(message):
|
| 20 |
+
|
| 21 |
+
finding_docs = yt_chunks.similarity_search(message, k = 5)
|
| 22 |
+
|
| 23 |
+
indices = [doc.metadata['row'] for doc in finding_docs]
|
| 24 |
+
|
| 25 |
+
retrievers = [json.loads(df.loc[idx].to_json(force_ascii = False)) for idx in indices]
|
| 26 |
+
|
| 27 |
+
return retrievers
|
| 28 |
+
|
| 29 |
+
def predict(message, history):
|
| 30 |
+
|
| 31 |
+
openai_input = list()
|
| 32 |
+
|
| 33 |
+
retriever = find_docs(message)
|
| 34 |
+
|
| 35 |
+
system_prompt = """- You are an AI chat bot that recommends YouTube content to users as an assistant.\n- You were created and powered by 'bigster (๋น
์คํฐ)', an AI & bigdata expert company.\n- Recommend YouTube content to users based on what's in โretrieverโ.\n- If the user's question is not related to content recommendations, please display a message declining to answer.\n- You must recommend at least 3 YouTube content items to the user based on the information in the 'retriever'. Be sure to explicitly include 'url' & 'videoChannelName' & 'videoName' information in your response. Also, for each featured piece of content, summarize what's in the 'transcription' and present it to the user. Use the following Markdown format to create hyperlinks: '[videoName](url)'\n\n retriever:\n{retriever}"""
|
| 36 |
+
|
| 37 |
+
for human, assistant in history:
|
| 38 |
+
openai_input.append({"role": "user", "content": human})
|
| 39 |
+
openai_input.append({"role": "assistant", "content": assistant})
|
| 40 |
+
|
| 41 |
+
openai_input = [item for item in openai_input if item['role'] != "system"]
|
| 42 |
+
|
| 43 |
+
openai_input.append({"role": "system", "content": system_prompt.format(retriever = retriever)})
|
| 44 |
+
openai_input.append({"role": "user", "content": message})
|
| 45 |
+
|
| 46 |
+
response = client.chat.completions.create(
|
| 47 |
+
model = MODEL,
|
| 48 |
+
messages = openai_input,
|
| 49 |
+
temperature = 1.0,
|
| 50 |
+
stream = True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
partial_message = ""
|
| 54 |
+
for chunk in response:
|
| 55 |
+
if chunk.choices[0].delta.content is not None:
|
| 56 |
+
partial_message = partial_message + chunk.choices[0].delta.content
|
| 57 |
+
yield partial_message
|
| 58 |
+
|
| 59 |
+
print(openai_input)
|
| 60 |
+
|
| 61 |
+
gr.ChatInterface(
|
| 62 |
+
predict,
|
| 63 |
+
title = "YOUTUBE REC",
|
| 64 |
+
theme = gr.themes.Soft(primary_hue = "purple"),
|
| 65 |
+
examples = [
|
| 66 |
+
"๋ค ์ด๋ฆ์ ๋ญ์ผ?",
|
| 67 |
+
"ํ์ด์ฌ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ฅผ ๋
ํํ๊ธฐ ์ํ ์์์ ์ถ์ฒํด์ค.",
|
| 68 |
+
"์ธ๊ฐ๊ด๊ณ์์ ํฐ ์์ค๊ฐ์ ๋๋ผ๋ ๋๋ฅผ ์ํ ์์์ ์ถ์ฒํด์ค.",
|
| 69 |
+
"๊ฐ๋จํ๊ณ ์ด๋ณด์๋ ์ดํดํ๊ธฐ ์ฌ์ด ๋ฅ๋ฌ๋ ๊ฐ์ ์ถ์ฒํด์ค.",
|
| 70 |
+
"ํ๋์ค ์ญ์ฌ์ ๋ํด ๊ณต๋ถํ๊ณ ์ถ์ด์. ํ๋์ค ์ญ์ฌ์ ๊ด๋ จ๋ ๋คํ๋ฉํฐ๋ฆฌ, ๊ฐ์ ์์์ ์ถ์ฒํด์ฃผ์ธ์.",
|
| 71 |
+
"์์ฆ ์์ธ์ด๋ฅผ ์์ฑํ๊ณ ๏ฟฝ๏ฟฝ์ต๋๋ค. ๋ณด๋ค ํจ๊ณผ์ ์ผ๋ก ๊ธ์ ์จ๋ด๋ ค๊ฐ๋ ๋ฐฉ๋ฒ์ ์ ์ํด์ฃผ๋ ์์์ ์ถ์ฒํด์ฃผ์ธ์."
|
| 72 |
+
]
|
| 73 |
+
).launch(share = True, auth = ("user", "bigster123"))
|