Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,61 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
import json
|
5 |
-
|
|
|
6 |
import pytz
|
7 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
import uuid
|
|
|
9 |
import extra_streamlit_components as stx
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
from urllib.parse import quote
|
|
|
|
|
|
|
12 |
from openai import OpenAI
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Initialize cookie manager
|
18 |
cookie_manager = stx.CookieManager()
|
@@ -20,6 +63,37 @@ cookie_manager = stx.CookieManager()
|
|
20 |
# File to store chat history and user data
|
21 |
CHAT_FILE = "chat_history.txt"
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# Function to save chat history and user data to file
|
24 |
def save_data():
|
25 |
with open(CHAT_FILE, 'w') as f:
|
@@ -63,13 +137,72 @@ def get_or_create_user():
|
|
63 |
|
64 |
return user
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
# ArXiv search function
|
75 |
def search_arxiv(query):
|
@@ -88,27 +221,6 @@ def search_arxiv(query):
|
|
88 |
else:
|
89 |
return "Error fetching results from ArXiv."
|
90 |
|
91 |
-
# Initialize OpenAI client
|
92 |
-
client = OpenAI(api_key=st.secrets['OPENAI_API_KEY'])
|
93 |
-
MODEL = "gpt-4-0125-preview" # Use the appropriate model
|
94 |
-
|
95 |
-
# Function to get AI response
|
96 |
-
def get_ai_response(prompt, context=""):
|
97 |
-
try:
|
98 |
-
messages = [
|
99 |
-
{"role": "system", "content": "You are a helpful assistant in a chat room that can also search ArXiv."},
|
100 |
-
{"role": "user", "content": f"Context: {context}\n\nUser Query: {prompt}"}
|
101 |
-
]
|
102 |
-
response = client.chat.completions.create(
|
103 |
-
model=MODEL,
|
104 |
-
messages=messages,
|
105 |
-
max_tokens=150,
|
106 |
-
temperature=0.7
|
107 |
-
)
|
108 |
-
return response.choices[0].message.content
|
109 |
-
except Exception as e:
|
110 |
-
return f"An error occurred: {str(e)}"
|
111 |
-
|
112 |
# Sidebar for user information and settings
|
113 |
with st.sidebar:
|
114 |
st.title("User Info")
|
@@ -138,12 +250,12 @@ for message in st.session_state.messages:
|
|
138 |
with st.chat_message(message["role"]):
|
139 |
st.markdown(message["content"])
|
140 |
|
141 |
-
#
|
142 |
-
if prompt := st.chat_input("
|
143 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
144 |
with st.chat_message("user"):
|
145 |
st.markdown(prompt)
|
146 |
-
|
147 |
# Check if it's an ArXiv search query
|
148 |
if prompt.lower().startswith("arxiv:"):
|
149 |
query = prompt[6:].strip()
|
@@ -153,18 +265,29 @@ if prompt := st.chat_input("Type your message or ArXiv search query:"):
|
|
153 |
st.markdown(f"Search results for '{query}':\n\n{search_results}")
|
154 |
|
155 |
# Get AI commentary on the search results
|
156 |
-
ai_commentary =
|
157 |
st.markdown(f"\nAI Analysis:\n{ai_commentary}")
|
158 |
|
159 |
st.session_state.messages.append({"role": "assistant", "content": f"Search results for '{query}':\n\n{search_results}\n\nAI Analysis:\n{ai_commentary}"})
|
160 |
else:
|
161 |
-
#
|
162 |
with st.chat_message("assistant"):
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
save_data()
|
169 |
st.rerun()
|
170 |
|
@@ -178,4 +301,62 @@ if 'last_refresh' not in st.session_state:
|
|
178 |
|
179 |
if time.time() - st.session_state.last_refresh > 5: # Refresh every 5 seconds
|
180 |
st.session_state.last_refresh = time.time()
|
181 |
-
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import cv2
|
3 |
+
import glob
|
4 |
import json
|
5 |
+
import math
|
6 |
+
import os
|
7 |
import pytz
|
8 |
+
import random
|
9 |
+
import re
|
10 |
+
import requests
|
11 |
+
import streamlit as st
|
12 |
+
import streamlit.components.v1 as components
|
13 |
+
import textract
|
14 |
+
import time
|
15 |
+
import zipfile
|
16 |
import uuid
|
17 |
+
import platform
|
18 |
import extra_streamlit_components as stx
|
19 |
+
|
20 |
+
from audio_recorder_streamlit import audio_recorder
|
21 |
+
from bs4 import BeautifulSoup
|
22 |
+
from collections import deque
|
23 |
+
from datetime import datetime
|
24 |
+
from dotenv import load_dotenv
|
25 |
+
from gradio_client import Client
|
26 |
+
from huggingface_hub import InferenceClient
|
27 |
+
from io import BytesIO
|
28 |
+
from moviepy.editor import VideoFileClip
|
29 |
+
from PIL import Image
|
30 |
+
from PyPDF2 import PdfReader
|
31 |
+
from templates import bot_template, css, user_template
|
32 |
from urllib.parse import quote
|
33 |
+
from xml.etree import ElementTree as ET
|
34 |
+
|
35 |
+
import openai
|
36 |
from openai import OpenAI
|
37 |
|
38 |
+
# Load environment variables
|
39 |
+
load_dotenv()
|
40 |
+
|
41 |
+
# Configuration
|
42 |
+
Site_Name = 'Scholarly-Article-Document-Search-With-Memory'
|
43 |
+
title = "๐ฌ๐ง ScienceBrain.AI"
|
44 |
+
helpURL = 'https://huggingface.co/awacke1'
|
45 |
+
bugURL = 'https://huggingface.co/spaces/awacke1'
|
46 |
+
icons = '๐ฌ'
|
47 |
+
|
48 |
+
st.set_page_config(
|
49 |
+
page_title=title,
|
50 |
+
page_icon=icons,
|
51 |
+
layout="wide",
|
52 |
+
initial_sidebar_state="auto",
|
53 |
+
menu_items={
|
54 |
+
'Get Help': helpURL,
|
55 |
+
'Report a bug': bugURL,
|
56 |
+
'About': title
|
57 |
+
}
|
58 |
+
)
|
59 |
|
60 |
# Initialize cookie manager
|
61 |
cookie_manager = stx.CookieManager()
|
|
|
63 |
# File to store chat history and user data
|
64 |
CHAT_FILE = "chat_history.txt"
|
65 |
|
66 |
+
# API configurations
|
67 |
+
API_URL = 'https://qe55p8afio98s0u3.us-east-1.aws.endpoints.huggingface.cloud'
|
68 |
+
API_KEY = st.secrets['API_KEY']
|
69 |
+
MODEL1 = "meta-llama/Llama-2-7b-chat-hf"
|
70 |
+
MODEL1URL = "https://huggingface.co/meta-llama/Llama-2-7b-chat-hf"
|
71 |
+
HF_KEY = st.secrets['HF_KEY']
|
72 |
+
headers = {
|
73 |
+
"Authorization": f"Bearer {HF_KEY}",
|
74 |
+
"Content-Type": "application/json"
|
75 |
+
}
|
76 |
+
|
77 |
+
# OpenAI client setup
|
78 |
+
client = OpenAI(api_key=st.secrets['OPENAI_API_KEY'], organization=st.secrets['OPENAI_ORG_ID'])
|
79 |
+
MODEL = "gpt-4-1106-preview"
|
80 |
+
|
81 |
+
# Session state initialization
|
82 |
+
if "openai_model" not in st.session_state:
|
83 |
+
st.session_state["openai_model"] = MODEL
|
84 |
+
if "messages" not in st.session_state:
|
85 |
+
st.session_state.messages = []
|
86 |
+
if "users" not in st.session_state:
|
87 |
+
st.session_state.users = []
|
88 |
+
if "current_user" not in st.session_state:
|
89 |
+
st.session_state.current_user = get_or_create_user()
|
90 |
+
|
91 |
+
# Sidebar configurations
|
92 |
+
should_save = st.sidebar.checkbox("๐พ Save", value=True, help="Save your session data.")
|
93 |
+
|
94 |
+
if st.sidebar.button("Clear Session"):
|
95 |
+
st.session_state.messages = []
|
96 |
+
|
97 |
# Function to save chat history and user data to file
|
98 |
def save_data():
|
99 |
with open(CHAT_FILE, 'w') as f:
|
|
|
137 |
|
138 |
return user
|
139 |
|
140 |
+
# HTML5 based Speech Synthesis (Text to Speech in Browser)
|
141 |
+
@st.cache_resource
|
142 |
+
def SpeechSynthesis(result):
|
143 |
+
documentHTML5 = f"""
|
144 |
+
<!DOCTYPE html>
|
145 |
+
<html>
|
146 |
+
<head>
|
147 |
+
<title>Read It Aloud</title>
|
148 |
+
<script type="text/javascript">
|
149 |
+
function readAloud() {{
|
150 |
+
const text = document.getElementById("textArea").value;
|
151 |
+
const speech = new SpeechSynthesisUtterance(text);
|
152 |
+
window.speechSynthesis.speak(speech);
|
153 |
+
}}
|
154 |
+
</script>
|
155 |
+
</head>
|
156 |
+
<body>
|
157 |
+
<h1>๐ Read It Aloud</h1>
|
158 |
+
<textarea id="textArea" rows="10" cols="80">{result}</textarea>
|
159 |
+
<br>
|
160 |
+
<button onclick="readAloud()">๐ Read Aloud</button>
|
161 |
+
</body>
|
162 |
+
</html>
|
163 |
+
"""
|
164 |
+
components.html(documentHTML5, width=1280, height=300)
|
165 |
+
|
166 |
+
# Function to generate filename
|
167 |
+
def generate_filename(prompt, file_type):
|
168 |
+
central = pytz.timezone('US/Central')
|
169 |
+
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
170 |
+
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
|
171 |
+
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:240]
|
172 |
+
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
173 |
+
|
174 |
+
# Function to process text
|
175 |
+
def process_text(text_input):
|
176 |
+
if text_input:
|
177 |
+
st.session_state.messages.append({"role": "user", "content": text_input})
|
178 |
+
with st.chat_message("user"):
|
179 |
+
st.markdown(text_input)
|
180 |
+
|
181 |
+
with st.chat_message("assistant"):
|
182 |
+
message_placeholder = st.empty()
|
183 |
+
full_response = ""
|
184 |
+
for response in client.chat.completions.create(
|
185 |
+
model=st.session_state["openai_model"],
|
186 |
+
messages=[
|
187 |
+
{"role": m["role"], "content": m["content"]}
|
188 |
+
for m in st.session_state.messages
|
189 |
+
],
|
190 |
+
stream=True,
|
191 |
+
):
|
192 |
+
full_response += (response.choices[0].delta.content or "")
|
193 |
+
message_placeholder.markdown(full_response + "โ")
|
194 |
+
message_placeholder.markdown(full_response)
|
195 |
+
|
196 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
197 |
+
filename = generate_filename(text_input, "md")
|
198 |
+
create_file(filename, text_input, full_response, should_save)
|
199 |
+
return full_response
|
200 |
+
|
201 |
+
# Function to create file
|
202 |
+
def create_file(filename, prompt, response, should_save=True):
|
203 |
+
if should_save:
|
204 |
+
with open(filename, "w", encoding="utf-8") as f:
|
205 |
+
f.write(prompt + "\n\n" + response)
|
206 |
|
207 |
# ArXiv search function
|
208 |
def search_arxiv(query):
|
|
|
221 |
else:
|
222 |
return "Error fetching results from ArXiv."
|
223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
# Sidebar for user information and settings
|
225 |
with st.sidebar:
|
226 |
st.title("User Info")
|
|
|
250 |
with st.chat_message(message["role"]):
|
251 |
st.markdown(message["content"])
|
252 |
|
253 |
+
# Chat input
|
254 |
+
if prompt := st.chat_input("What would you like to know?"):
|
255 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
256 |
with st.chat_message("user"):
|
257 |
st.markdown(prompt)
|
258 |
+
|
259 |
# Check if it's an ArXiv search query
|
260 |
if prompt.lower().startswith("arxiv:"):
|
261 |
query = prompt[6:].strip()
|
|
|
265 |
st.markdown(f"Search results for '{query}':\n\n{search_results}")
|
266 |
|
267 |
# Get AI commentary on the search results
|
268 |
+
ai_commentary = process_text(f"Provide a brief analysis of these ArXiv search results: {search_results}")
|
269 |
st.markdown(f"\nAI Analysis:\n{ai_commentary}")
|
270 |
|
271 |
st.session_state.messages.append({"role": "assistant", "content": f"Search results for '{query}':\n\n{search_results}\n\nAI Analysis:\n{ai_commentary}"})
|
272 |
else:
|
273 |
+
# Regular chat processing
|
274 |
with st.chat_message("assistant"):
|
275 |
+
message_placeholder = st.empty()
|
276 |
+
full_response = ""
|
277 |
+
for response in client.chat.completions.create(
|
278 |
+
model=st.session_state["openai_model"],
|
279 |
+
messages=[
|
280 |
+
{"role": m["role"], "content": m["content"]}
|
281 |
+
for m in st.session_state.messages
|
282 |
+
],
|
283 |
+
stream=True,
|
284 |
+
):
|
285 |
+
full_response += (response.choices[0].delta.content or "")
|
286 |
+
message_placeholder.markdown(full_response + "โ")
|
287 |
+
message_placeholder.markdown(full_response)
|
288 |
+
|
289 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
290 |
+
|
291 |
save_data()
|
292 |
st.rerun()
|
293 |
|
|
|
301 |
|
302 |
if time.time() - st.session_state.last_refresh > 5: # Refresh every 5 seconds
|
303 |
st.session_state.last_refresh = time.time()
|
304 |
+
st.rerun()
|
305 |
+
|
306 |
+
# Main function to handle different input types
|
307 |
+
def main():
|
308 |
+
st.markdown("##### GPT-4 Multimodal AI Assistant: Text, Audio, Image, & Video")
|
309 |
+
option = st.selectbox("Select an option", ("Text", "Image", "Audio", "Video"))
|
310 |
+
|
311 |
+
if option == "Text":
|
312 |
+
text_input = st.text_input("Enter your text:")
|
313 |
+
if text_input:
|
314 |
+
process_text(text_input)
|
315 |
+
|
316 |
+
elif option == "Image":
|
317 |
+
text = "Help me understand what is in this picture and list ten facts as markdown outline with appropriate emojis that describes what you see."
|
318 |
+
text_input = st.text_input(label="Enter text prompt to use with Image context.", value=text)
|
319 |
+
image_input = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
320 |
+
if image_input:
|
321 |
+
image = Image.open(image_input)
|
322 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
323 |
+
if st.button("Analyze Image"):
|
324 |
+
with st.spinner("Analyzing..."):
|
325 |
+
image_byte_arr = BytesIO()
|
326 |
+
image.save(image_byte_arr, format='PNG')
|
327 |
+
image_byte_arr = image_byte_arr.getvalue()
|
328 |
+
response = client.chat.completions.create(
|
329 |
+
model="gpt-4-vision-preview",
|
330 |
+
messages=[
|
331 |
+
{
|
332 |
+
"role": "user",
|
333 |
+
"content": [
|
334 |
+
{"type": "text", "text": text_input},
|
335 |
+
{
|
336 |
+
"type": "image_url",
|
337 |
+
"image_url": {
|
338 |
+
"url": f"data:image/jpeg;base64,{base64.b64encode(image_byte_arr).decode()}"
|
339 |
+
}
|
340 |
+
},
|
341 |
+
],
|
342 |
+
}
|
343 |
+
],
|
344 |
+
max_tokens=300,
|
345 |
+
)
|
346 |
+
st.write(response.choices[0].message.content)
|
347 |
+
|
348 |
+
elif option == "Audio":
|
349 |
+
text = "You are generating a transcript summary. Create a summary of the provided transcription. Respond in Markdown."
|
350 |
+
text_input = st.text_input(label="Enter text prompt to use with Audio context.", value=text)
|
351 |
+
audio_file = st.file_uploader("Upload an audio file", type=["mp3", "wav"])
|
352 |
+
if audio_file:
|
353 |
+
if st.button("Transcribe Audio"):
|
354 |
+
with st.spinner("Transcribing..."):
|
355 |
+
transcription = client.audio.transcriptions.create(
|
356 |
+
model="whisper-1",
|
357 |
+
file=audio_file
|
358 |
+
)
|
359 |
+
st.write(transcription.text)
|
360 |
+
st.session_state.messages.append({"role": "user", "content": f"Transcription: {transcription.text}"})
|
361 |
+
process_text(f"{text}\n\nTranscription: {transcription.text}")
|
362 |
+
|