drlee1 commited on
Commit
c571641
โ€ข
1 Parent(s): a173031

initial commit

Browse files
Files changed (1) hide show
  1. app.py +72 -62
app.py CHANGED
@@ -1,63 +1,73 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
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"))