Spaces:
Running
Running
import streamlit as st | |
import openai | |
import os | |
import base64 | |
import glob | |
import json | |
import mistune | |
import pytz | |
import math | |
import requests | |
from datetime import datetime | |
from openai import ChatCompletion | |
from xml.etree import ElementTree as ET | |
from bs4 import BeautifulSoup | |
from collections import deque | |
from audio_recorder_streamlit import audio_recorder | |
# Function Definitions (kept unchanged) | |
# ... | |
def chat_with_file_contents(prompt, file_content): | |
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}] | |
conversation.append({'role': 'user', 'content': prompt}) | |
if len(file_content)>0: | |
conversation.append({'role': 'assistant', 'content': file_content}) | |
response = openai.ChatCompletion.create(model=model_choice, messages=conversation) | |
return response['choices'][0]['message']['content'] | |
# Sidebar and global | |
openai.api_key = os.getenv('OPENAI_KEY') | |
st.set_page_config(page_title="GPT Streamlit Document Reasoner",layout="wide") | |
menu = ["htm", "txt", "xlsx", "csv", "md", "py"] #619 | |
choice = st.sidebar.selectbox("Output File Type:", menu) | |
model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301')) | |
# Audio, transcribe, GPT: | |
filename = save_and_play_audio(audio_recorder) | |
if filename is not None: | |
transcription = transcribe_audio(openai.api_key, filename, "whisper-1") | |
st.write(transcription) | |
gptOutput = chat_with_model(transcription, '') | |
filename = generate_filename(transcription, choice) | |
create_file(filename, transcription, gptOutput) | |
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True) | |
def main(): | |
user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100) | |
collength, colupload = st.columns([2,3]) # adjust the ratio as needed | |
with collength: | |
max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000) | |
with colupload: | |
uploaded_file = st.file_uploader("Add a file for context:", type=["xml", "json", "xlsx","csv","html", "htm", "md", "txt"]) | |
document_sections = deque() | |
document_responses = {} | |
if uploaded_file is not None: | |
file_content = read_file_content(uploaded_file, max_length) | |
document_sections.extend(divide_document(file_content, max_length)) | |
if len(document_sections) > 0: | |
if st.button("👁️ View Upload"): | |
st.markdown("**Sections of the uploaded file:**") | |
for i, section in enumerate(list(document_sections)): | |
st.markdown(f"**Section {i+1}**\n{section}") | |
st.markdown("**Chat with the model:**") | |
for i, section in enumerate(list(document_sections)): | |
if i in document_responses: | |
st.markdown(f"**Section {i+1}**\n{document_responses[i]}") | |
else: | |
if st.button(f"Chat about Section {i+1}"): | |
st.write('Reasoning with your inputs...') | |
response = chat_with_model(user | |