File size: 3,154 Bytes
34b6334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7a1c50
34b6334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8daada9
aef45b9
34b6334
 
 
 
 
6ee2f11
1
2
3
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import gradio as gr
import os
import torch
from pathlib import Path
import whisper
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.llms import OpenAI
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.chains import RetrievalQA
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

def process_meeting(api_key, topic, audio_file):
    os.environ["OPENAI_API_KEY"] = api_key

    # Set the filename to the uploaded audio file's name without extension
    ffname, _ = os.path.splitext(audio_file.name)
    audio_file_path = ffname + ".m4a"

    # Save the uploaded audio file
    with open(audio_file_path, 'wb') as f:
        f.write(audio_file.read())

    file = audio_file_path
    DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
    model = whisper.load_model("medium.en").to(DEVICE)

    def transcribe_file(model, file):    
        file_path = Path(file)
        print(f"Transcribing file: {file_path}\n")

        result = model.transcribe(file, verbose = False, language = "en")
                  
        return result["text"]

    transcribed_text = transcribe_file(model, file)

    loader = TextLoader(transcribed_text, encoding="utf8")
    doc = loader.load()

    text_splitter = CharacterTextSplitter(        
            separator = "\n",
            chunk_size = 1024,
            chunk_overlap  = 50,
            length_function = len,
        )

    llmChainName = "text-davinci-003"

    llm = OpenAI(
        model_name=llmChainName,
        streaming=True,
        verbose=True,
        temperature=0
    )

    embeddings = OpenAIEmbeddings()
    persist_directory = './chroma'
    texts = text_splitter.split_documents(doc)
    store = Chroma.from_documents(texts, embeddings, collection_name="meeting", persist_directory=persist_directory)

    llm = ChatOpenAI(
        model_name="gpt-4",
        temperature=0,
        streaming=True,
        verbose=False,
        callbacks=[StreamingStdOutCallbackHandler()]
    )

    chain_Instructions = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=store.as_retriever(search_kwargs={"k": 1}))

    meeting_summary = chain_Instructions.run("Please summarize the meeting and provide the minutes. Include actions, responsible persons, and due dates where possible. Provide a bullet list for actions. The topic is: " + topic)

    try:
        store.persist()
        del store
    except Exception as e:
        print(e)

    return meeting_summary

# Define the Gradio interface
iface = gr.Interface(
    fn=process_meeting, 
    inputs=[
        gr.inputs.Textbox(lines=1, label="OpenAI API Key"),
        gr.inputs.Textbox(lines=2, label="Meeting Topic"),
        gr.inputs.File(label="Upload Audio"),
    ], 
    outputs=gr.outputs.Textbox(label="Meeting Minutes"),
    live=False,
)

# Launch the app
iface.launch()