joseluhf11 commited on
Commit
dda0b8c
·
1 Parent(s): 6867599

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -33
app.py CHANGED
@@ -6,7 +6,7 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
6
  from langchain.embeddings import AwaEmbeddings
7
  import os
8
  import openai
9
- import gradio as gr
10
 
11
 
12
  def get_transcript(url):
@@ -37,36 +37,72 @@ text_splitter = RecursiveCharacterTextSplitter(
37
  is_separator_regex = False,
38
  )
39
 
40
- def chat(url, question, api_key):
41
- os.environ["OPENAI_API_KEY"] = api_key
42
- openai.api_key = api_key
43
-
44
- info = get_transcript(url)
45
- texts = text_splitter.create_documents([info])
46
- db = FAISS.from_documents(texts, embeddings)
47
- docs = db.similarity_search(question)
48
-
49
- prompt = [
50
- {"role": "system", "content": """You are my Youtube Asisstant. I will pass you texts from a Youtube Video Transcrip and I need you to use them to answer my question from the Youtube Video.
51
- Please do not invent any information, and I am asking about information in the Youtube Video."""},
52
- {"role":"user", "content": f"Context:{docs}"},
53
- {"role":"user", "content": f"Question:{question}"},
54
- ]
55
- response = openai.ChatCompletion.create(
56
- model="gpt-3.5-turbo-0613",
57
- messages=prompt,
58
- temperature = 0
59
- )
60
-
61
- return response["choices"][0]["message"]["content"]
62
-
63
-
64
- # Create a gradio interface with two inputs and one output
65
- demo = gr.Interface(
66
- fn=chat, # The function to call
67
- inputs=[gr.Textbox(label="Youtube URL"), gr.Textbox(label="Question"), gr.Textbox(label="API_KEY")], # The input components
68
- outputs=gr.Textbox(label="Answer") # The output component
69
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- # Launch the interface
72
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
6
  from langchain.embeddings import AwaEmbeddings
7
  import os
8
  import openai
9
+ import streamlit as st
10
 
11
 
12
  def get_transcript(url):
 
37
  is_separator_regex = False,
38
  )
39
 
40
+ def chat(question):
41
+ if 'database' in st.session_state:
42
+ docs = st.session_state.database.similarity_search(question)
43
+
44
+ prompt = [
45
+ {"role": "system", "content": """You are my Youtube Asisstant. I will pass you texts from a Youtube Video Transcrip and I need you to use them to answer my question from the Youtube Video.
46
+ Please do not invent any information, and I am asking about information in the Youtube Video."""},
47
+ {"role":"user", "content": f"Context:{docs}"},
48
+ {"role":"user", "content": f"Question:{question}"},
49
+ ]
50
+ response = openai.ChatCompletion.create(
51
+ model="gpt-3.5-turbo-0613",
52
+ messages=prompt,
53
+ temperature = 0
54
+ )
55
+
56
+ return response["choices"][0]["message"]["content"]
57
+ else:
58
+ return "Error, not generated database"
59
+
60
+
61
+ #------------------------------------------------------------- APP STREAMLIT--------------------------------------------------------------------
62
+
63
+ st.title("Ask Question Youtube Videos")
64
+
65
+ with st.sidebar:
66
+ if "api" not in st.session_state:
67
+ api_key= st.text_input(label="api", placeholder="API Key from OpenAI", label_visibility="hidden")
68
+ if st.button(label="Save"):
69
+ os.environ["OPENAI_API_KEY"] = api_key
70
+ openai.api_key = api_key
71
+ st.session_state['api'] = api_key
72
+ else:
73
+ url = st.text_input(label="url", placeholder="Youtube Video URL", label_visibility="hidden")
74
+ if st.button(label="Save"):
75
+ st.session_state['url'] = url
76
+ info = get_transcript(url)
77
+ texts = text_splitter.create_documents([info])
78
+ st.session_state['database'] = FAISS.from_documents(texts, embeddings)
79
+
80
+ if "api" not in st.session_state:
81
+ st.write("Please, introduce your OpenAI API key to ask questions to any YouTube Video")
82
+
83
+ elif 'url' not in st.session_state:
84
+ st.write("Please, introduce link URL from the YouTube Video")
85
+
86
+ else:
87
+ # Initialize chat history
88
+ if "messages" not in st.session_state:
89
+ st.session_state.messages = []
90
+
91
+ # Display chat messages from history on app rerun
92
+ for message in st.session_state.messages:
93
+ with st.chat_message(message["role"]):
94
+ st.markdown(message["content"])
95
 
96
+ # Accept user input
97
+ if prompt := st.chat_input("What is up?"):
98
+ # Add user message to chat history
99
+ st.session_state.messages.append({"role": "user", "content": prompt})
100
+ # Display user message in chat message container
101
+ with st.chat_message("user"):
102
+ st.markdown(prompt)
103
+ # Get response from your custom chat function
104
+ response = chat(prompt)
105
+ # Display assistant response in chat message container
106
+ with st.chat_message("assistant"):
107
+ st.markdown(response)
108
+ st.session_state.messages.append({"role": "assistant", "content": response})