forked from Azure-Samples/miyagi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (40 loc) · 1.49 KB
/
main.py
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
import os
import gradio as gr
import openai
from pydantic import BaseSettings, BaseModel
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
AIMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage
)
GPT_TURBO = "gpt-3.5-turbo"
class Settings(BaseSettings):
openai.api_key = os.getenv("OPENAI_API_KEY")
settings = Settings()
def transcribe(text):
"""
Use Langchain's LLMChain to generate a transcript from text
text: str
If it fails, throws an exception.
"""
chat = ChatOpenAI(temperature=0)
template = "You are a helpful financial advisor. Respond in the voice of Jim Cramer from Mad Money."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
example_human = HumanMessagePromptTemplate.from_template("Hi!")
example_ai = AIMessagePromptTemplate.from_template("Boo-yah! Are you ready, skee-daddy?")
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages(
[system_message_prompt, example_human, example_ai, human_message_prompt])
chain = LLMChain(llm=chat, prompt=chat_prompt)
return chain.run(text)
ui = gr.Interface(fn=transcribe, inputs="text", outputs="text").launch()
ui.launch()