Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,13 @@
|
|
1 |
-
pip install torch torchvision torchaudio
|
2 |
import streamlit as st
|
3 |
import pandas as pd
|
|
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
import speech_recognition as sr
|
6 |
|
7 |
# Load the Netflix dataset from CSV
|
8 |
@st.cache_data
|
9 |
def load_data():
|
10 |
-
|
11 |
-
return pd.read_csv(url)
|
12 |
|
13 |
# Load DialoGPT model and tokenizer
|
14 |
@st.cache_resource
|
@@ -17,32 +16,32 @@ def load_model():
|
|
17 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
18 |
return tokenizer, model
|
19 |
|
20 |
-
# Function to search movie details
|
21 |
def search_movie_details(query, data):
|
22 |
query = query.lower()
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
28 |
return results
|
29 |
|
30 |
# Function to convert voice to text
|
31 |
def voice_to_text():
|
32 |
recognizer = sr.Recognizer()
|
33 |
with sr.Microphone() as source:
|
34 |
-
st.write("
|
|
|
35 |
try:
|
36 |
-
|
37 |
-
text = recognizer.recognize_google(audio)
|
38 |
-
return text
|
39 |
except sr.UnknownValueError:
|
40 |
return "Sorry, I could not understand the audio."
|
41 |
except sr.RequestError:
|
42 |
return "Sorry, the speech service is down."
|
43 |
|
44 |
# Streamlit App
|
45 |
-
st.title("
|
46 |
|
47 |
# Load dataset and model
|
48 |
data = load_data()
|
@@ -55,29 +54,28 @@ user_input = ""
|
|
55 |
if input_option == "Text":
|
56 |
user_input = st.text_input("Enter the movie name, director, or cast:")
|
57 |
elif input_option == "Voice":
|
58 |
-
if st.button("
|
59 |
user_input = voice_to_text()
|
60 |
-
st.write(f"
|
61 |
|
62 |
# Generate response
|
63 |
if user_input:
|
64 |
-
# Search for movie details
|
65 |
movie_results = search_movie_details(user_input, data)
|
66 |
|
67 |
if not movie_results.empty:
|
68 |
-
st.write("
|
69 |
for _, row in movie_results.iterrows():
|
70 |
-
st.write(f"
|
71 |
-
st.write(f"
|
72 |
-
st.write(f"
|
73 |
-
st.write(f"
|
74 |
-
st.write(f"
|
75 |
-
st.write(f"
|
76 |
-
st.write(f"
|
77 |
st.write("---")
|
78 |
else:
|
79 |
# Use DialoGPT for general conversation
|
80 |
inputs = tokenizer.encode(user_input, return_tensors="pt")
|
81 |
outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
|
82 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
83 |
-
st.write(f"
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import torch # Ensure PyTorch is imported
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
import speech_recognition as sr
|
6 |
|
7 |
# Load the Netflix dataset from CSV
|
8 |
@st.cache_data
|
9 |
def load_data():
|
10 |
+
return pd.read_csv("https://huggingface.co/spaces/mfraz/Netflix-data/resolve/main/netflix_titles.csv")
|
|
|
11 |
|
12 |
# Load DialoGPT model and tokenizer
|
13 |
@st.cache_resource
|
|
|
16 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
17 |
return tokenizer, model
|
18 |
|
19 |
+
# Function to search the dataset for movie details
|
20 |
def search_movie_details(query, data):
|
21 |
query = query.lower()
|
22 |
+
filtered_data = data.dropna(subset=["title", "cast", "director"]) # Remove NaN values
|
23 |
+
results = filtered_data[
|
24 |
+
filtered_data["title"].str.lower().str.contains(query, na=False) |
|
25 |
+
filtered_data["cast"].str.lower().str.contains(query, na=False) |
|
26 |
+
filtered_data["director"].str.lower().str.contains(query, na=False)
|
27 |
+
]
|
28 |
return results
|
29 |
|
30 |
# Function to convert voice to text
|
31 |
def voice_to_text():
|
32 |
recognizer = sr.Recognizer()
|
33 |
with sr.Microphone() as source:
|
34 |
+
st.write("Speak now...")
|
35 |
+
audio = recognizer.listen(source)
|
36 |
try:
|
37 |
+
return recognizer.recognize_google(audio)
|
|
|
|
|
38 |
except sr.UnknownValueError:
|
39 |
return "Sorry, I could not understand the audio."
|
40 |
except sr.RequestError:
|
41 |
return "Sorry, the speech service is down."
|
42 |
|
43 |
# Streamlit App
|
44 |
+
st.title("Netflix Movie Details Chatbot π¬")
|
45 |
|
46 |
# Load dataset and model
|
47 |
data = load_data()
|
|
|
54 |
if input_option == "Text":
|
55 |
user_input = st.text_input("Enter the movie name, director, or cast:")
|
56 |
elif input_option == "Voice":
|
57 |
+
if st.button("Start Recording"):
|
58 |
user_input = voice_to_text()
|
59 |
+
st.write(f"You said: {user_input}")
|
60 |
|
61 |
# Generate response
|
62 |
if user_input:
|
|
|
63 |
movie_results = search_movie_details(user_input, data)
|
64 |
|
65 |
if not movie_results.empty:
|
66 |
+
st.write("Here are the matching results:")
|
67 |
for _, row in movie_results.iterrows():
|
68 |
+
st.write(f"**Title:** {row['title']}")
|
69 |
+
st.write(f"**Type:** {row['type']}")
|
70 |
+
st.write(f"**Director:** {row['director']}")
|
71 |
+
st.write(f"**Cast:** {row['cast']}")
|
72 |
+
st.write(f"**Release Year:** {row['release_year']}")
|
73 |
+
st.write(f"**Rating:** {row['rating']}")
|
74 |
+
st.write(f"**Description:** {row['description']}")
|
75 |
st.write("---")
|
76 |
else:
|
77 |
# Use DialoGPT for general conversation
|
78 |
inputs = tokenizer.encode(user_input, return_tensors="pt")
|
79 |
outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
|
80 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
81 |
+
st.write(f"Chatbot: {response}")
|