Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import speech_recognition as sr
|
5 |
+
from pydub import AudioSegment
|
6 |
+
|
7 |
+
# Load the Netflix dataset from CSV
|
8 |
+
@st.cache_data
|
9 |
+
def load_data():
|
10 |
+
return pd.read_csv("netflix_titles.csv")
|
11 |
+
|
12 |
+
# Load DialoGPT model and tokenizer
|
13 |
+
@st.cache_resource
|
14 |
+
def load_model():
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
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 |
+
results = data[
|
23 |
+
data["title"].str.lower().str.contains(query) |
|
24 |
+
data["cast"].str.lower().str.contains(query) |
|
25 |
+
data["director"].str.lower().str.contains(query)
|
26 |
+
]
|
27 |
+
return results
|
28 |
+
|
29 |
+
# Function to convert voice to text
|
30 |
+
def voice_to_text():
|
31 |
+
recognizer = sr.Recognizer()
|
32 |
+
with sr.Microphone() as source:
|
33 |
+
st.write("Speak now...")
|
34 |
+
audio = recognizer.listen(source)
|
35 |
+
try:
|
36 |
+
text = recognizer.recognize_google(audio)
|
37 |
+
return text
|
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()
|
48 |
+
tokenizer, model = load_model()
|
49 |
+
|
50 |
+
# Input options: Text or Voice
|
51 |
+
input_option = st.radio("Choose input method:", ("Text", "Voice"))
|
52 |
+
|
53 |
+
user_input = ""
|
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 |
+
# Search for movie details
|
64 |
+
movie_results = search_movie_details(user_input, data)
|
65 |
+
|
66 |
+
if not movie_results.empty:
|
67 |
+
st.write("Here are the matching results:")
|
68 |
+
for _, row in movie_results.iterrows():
|
69 |
+
st.write(f"**Title:** {row['title']}")
|
70 |
+
st.write(f"**Type:** {row['type']}")
|
71 |
+
st.write(f"**Director:** {row['director']}")
|
72 |
+
st.write(f"**Cast:** {row['cast']}")
|
73 |
+
st.write(f"**Release Year:** {row['release_year']}")
|
74 |
+
st.write(f"**Rating:** {row['rating']}")
|
75 |
+
st.write(f"**Description:** {row['description']}")
|
76 |
+
st.write("---")
|
77 |
+
else:
|
78 |
+
# Use DialoGPT for general conversation
|
79 |
+
inputs = tokenizer.encode(user_input, return_tensors="pt")
|
80 |
+
outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
|
81 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
82 |
+
st.write(f"Chatbot: {response}")
|