Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from gradio_client import Client
|
3 |
+
from llama_index.llms import Replicate
|
4 |
+
from llama_index.embeddings import LangchainEmbedding
|
5 |
+
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
6 |
+
from llama_index import set_global_service_context, ServiceContext, VectorStoreIndex, SimpleDirectoryReader
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Ensure the environment variable is set
|
10 |
+
if "REPLICATE_API_TOKEN" not in os.environ:
|
11 |
+
raise ValueError("Please set the REPLICATE_API_TOKEN environment variable.")
|
12 |
+
else:
|
13 |
+
os.environ["REPLICATE_API_TOKEN"] = os.environ["REPLICATE_API_TOKEN"]
|
14 |
+
|
15 |
+
PATH = '/Data'
|
16 |
+
|
17 |
+
llm = Replicate(
|
18 |
+
model="replicate/vicuna-13b:6282abe6a492de4145d7bb601023762212f9ddbbe78278bd6771c8b3b2f2a13b"
|
19 |
+
)
|
20 |
+
|
21 |
+
embeddings = LangchainEmbedding(
|
22 |
+
HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
23 |
+
)
|
24 |
+
|
25 |
+
service_context = ServiceContext.from_defaults(
|
26 |
+
chunk_size=1024,
|
27 |
+
llm=llm,
|
28 |
+
embed_model=embeddings
|
29 |
+
)
|
30 |
+
set_global_service_context(service_context)
|
31 |
+
|
32 |
+
# Transcribe and Query function
|
33 |
+
def transcribe_and_query(youtube_url, message):
|
34 |
+
client = Client("https://sanchit-gandhi-whisper-jax.hf.space/")
|
35 |
+
result = client.predict(youtube_url, "transcribe", True, fn_index=7)
|
36 |
+
with open(f'{PATH}/docs.txt','w') as f:
|
37 |
+
f.write(result[1])
|
38 |
+
|
39 |
+
documents = SimpleDirectoryReader(PATH).load_data()
|
40 |
+
index = VectorStoreIndex.from_documents(documents)
|
41 |
+
query_engine = index.as_query_engine()
|
42 |
+
response = query_engine.query(message)
|
43 |
+
|
44 |
+
# Assuming the response has a 'response' attribute with the answer
|
45 |
+
return response.response
|
46 |
+
|
47 |
+
# Streamlit UI
|
48 |
+
st.title("YouTube Video Chatbot")
|
49 |
+
|
50 |
+
# Input for YouTube URL
|
51 |
+
youtube_url = st.text_input("Enter YouTube Video URL:")
|
52 |
+
|
53 |
+
# Chatbot UI
|
54 |
+
if "messages" not in st.session_state:
|
55 |
+
st.session_state.messages = []
|
56 |
+
|
57 |
+
# Display chat history
|
58 |
+
for message in st.session_state.messages:
|
59 |
+
if message["role"] == "human":
|
60 |
+
st.write(f"You: {message['content']}")
|
61 |
+
else:
|
62 |
+
st.write(f"Chatbot: {message['content']}")
|
63 |
+
|
64 |
+
# User input
|
65 |
+
prompt = st.text_input("Ask something about the video:")
|
66 |
+
|
67 |
+
# React to user input
|
68 |
+
if prompt:
|
69 |
+
# Add user message to chat history
|
70 |
+
st.session_state.messages.append({"role": "human", "content": prompt})
|
71 |
+
|
72 |
+
# Get response from the chatbot
|
73 |
+
response = transcribe_and_query(youtube_url, prompt)
|
74 |
+
|
75 |
+
# Add assistant response to chat history
|
76 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
77 |
+
|
78 |
+
# Refresh the page to show the updated chat history
|
79 |
+
if prompt:
|
80 |
+
st.experimental_rerun()
|