tenet commited on
Commit
24c37ee
·
verified ·
1 Parent(s): c2d8316

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -50
app.py CHANGED
@@ -1,51 +1,48 @@
1
  import streamlit as st
2
- from PIL import Image
3
- from transformers import CLIPProcessor, CLIPModel
4
- import os
5
-
6
- # Initialize Hugging Face Model
7
- processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
8
- model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
9
-
10
- # Streamlit app layout
11
- st.title("AI Multimodal File Processing App")
12
- st.write("This app uses Hugging Face's CLIP model to analyze and edit image and text files.")
13
-
14
- # Option to upload an image
15
- st.subheader("Upload an Image")
16
- uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
17
-
18
- if uploaded_image is not None:
19
- image = Image.open(uploaded_image)
20
- st.image(image, caption="Uploaded Image", use_column_width=True)
21
-
22
- text = st.text_input("Enter text for image description", "A picture of a cat") # Default text
23
-
24
- if st.button("Analyze Image"):
25
- # Process the image with CLIP
26
- inputs = processor(text=text, images=image, return_tensors="pt", padding=True)
27
- outputs = model(**inputs)
28
-
29
- # Display the similarity score between text and image
30
- logits_per_image = outputs.logits_per_image # Similarity score
31
- st.write(f"Similarity Score: {logits_per_image.item()}")
32
- st.write(f"Description: {text}")
33
-
34
- # Option to upload a text file
35
- st.subheader("Upload a Text File")
36
- uploaded_textfile = st.file_uploader("Choose a text file...", type=["txt"])
37
-
38
- if uploaded_textfile is not None:
39
- text_data = uploaded_textfile.read().decode('utf-8')
40
- st.text_area("Text File Content", text_data, height=200)
41
-
42
- if st.button("Process Text"):
43
- # Example: Modify the text (e.g., convert to uppercase)
44
- processed_text = text_data.upper()
45
- st.write("Processed Text:")
46
- st.text_area("Processed Text Output", processed_text, height=200)
47
-
48
- # Optional: File download button
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)