import gradio as gr from transformers import pipeline import numpy as np import os from langchain.chat_models import ChatOpenAI from langchain import ConversationChain, LLMChain, PromptTemplate from langchain.memory import ConversationBufferWindowMemory # from flask_cors import CORS from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Chroma from langchain.embeddings import OpenAIEmbeddings # from flask_cors import CORS, cross_origin from langchain.chains import RetrievalQA # from langchain.llms import OpenAI # from langchain_community.chat_models import ChatOpenAI # from PyPDF2 import PdfReader # from typing_extensions import Concatenate from langchain.chat_models import ChatOpenAI from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from langchain.schema import HumanMessage, SystemMessage # from flask import Flask, request, jsonify import os transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en") def predict(user_input): customer_service_prompt = """ Create an email response based on the content of incoming emails. Your response should be tailored to the nature of the inquiry: If the email mentions 'customer support,' reply with a message indicating that their request has been forwarded, and your team will respond within 48 hours. Include the support email address for further communication. If the email indicates the customer is looking for new products, provide a response that shares a list of available products and directs them to your website for more information. For payment-related inquiries, respond by informing the customer that your finance team will review their request and get back to them within the next 48 hours. Include the finance team's email address for any additional communication. Ensure that the responses are clear, polite, and informative. Use appropriate language and tone for each scenario." User's Query: {user_query} Mail reply: """ template = customer_service_prompt.format(user_query=user_input) prompt = PromptTemplate( input_variables=["user_query"], template=template) inputs = {"input": ""} chatgpt_llm = ChatOpenAI(model_name='gpt-3.5-turbo-16k', temperature=0, openai_api_key='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') chatgpt_chain = LLMChain( llm=chatgpt_llm, prompt=prompt) response = chatgpt_chain.run(inputs) return response def transcribe(audio): sr, y = audio y = y.astype(np.float32) y /= np.max(np.abs(y)) results = predict(transcriber({"sampling_rate": sr, "raw": y})["text"]) return results demo = gr.Interface( transcribe, gr.Audio(sources=["microphone"]), "text", ) demo.launch()