Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,48 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
if uploaded_image is not None or uploaded_textfile is not None:
|
50 |
-
st.download_button("Download Processed File", data="Some file data here", file_name="processed_file.txt")
|
51 |
-
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
+
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, WebRtcMode
|
4 |
+
|
5 |
+
# Load the pretrained DialoGPT model
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
7 |
+
model = GPT2LMHeadModel.from_pretrained("microsoft/DialoGPT-medium")
|
8 |
+
|
9 |
+
# Streamlit UI Setup
|
10 |
+
st.title("AI Multimodal Chat & File Processing App")
|
11 |
+
|
12 |
+
# Chat history session state setup
|
13 |
+
if "history" not in st.session_state:
|
14 |
+
st.session_state.history = []
|
15 |
+
|
16 |
+
# Function to process the chat
|
17 |
+
def chat_with_model(user_input):
|
18 |
+
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
19 |
+
st.session_state.history.append(new_user_input_ids)
|
20 |
+
|
21 |
+
bot_input_ids = new_user_input_ids
|
22 |
+
for history in st.session_state.history:
|
23 |
+
bot_input_ids = history if len(history) < 2048 else history[-1024:]
|
24 |
+
|
25 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
26 |
+
bot_output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
27 |
+
return bot_output
|
28 |
+
|
29 |
+
# Chat Input Box
|
30 |
+
user_input = st.text_input("You: ", "")
|
31 |
+
if user_input:
|
32 |
+
response = chat_with_model(user_input)
|
33 |
+
st.session_state.history.append(tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt"))
|
34 |
+
st.write(f"Bot: {response}")
|
35 |
+
|
36 |
+
# Show chat history
|
37 |
+
if st.session_state.history:
|
38 |
+
for i in range(len(st.session_state.history) - 1, -1, -1):
|
39 |
+
user_msg = tokenizer.decode(st.session_state.history[i], skip_special_tokens=True)
|
40 |
+
st.write(f"You: {user_msg}")
|
41 |
+
|
42 |
+
# Video/Audio Stream
|
43 |
+
st.subheader("Video/Audio Stream")
|
44 |
+
class VideoProcessor(VideoProcessorBase):
|
45 |
+
def recv(self, frame):
|
46 |
+
return frame
|
47 |
+
|
48 |
+
webrtc_streamer(key="example", mode=WebRtcMode.SENDRECV, video_processor_factory=VideoProcessor)
|
|
|
|
|
|