Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain.chat_models import ChatLiteLLM
|
4 |
+
from langchain.prompts.chat import (
|
5 |
+
ChatPromptTemplate,
|
6 |
+
SystemMessagePromptTemplate,
|
7 |
+
AIMessagePromptTemplate,
|
8 |
+
HumanMessagePromptTemplate,
|
9 |
+
)
|
10 |
+
from langchain.schema import AIMessage, HumanMessage, SystemMessage
|
11 |
+
|
12 |
+
os.environ['PALM_API_KEY'] = "AIzaSyBcSZ82EePKjrgZphWXMpKP3u3Kcfz_8AU" # Replace with your key
|
13 |
+
|
14 |
+
chat = ChatLiteLLM(model="palm/chat-bison")
|
15 |
+
|
16 |
+
def predict(message, history):
|
17 |
+
history_langchain_format = []
|
18 |
+
for human, ai in history:
|
19 |
+
history_langchain_format.append(HumanMessage(content=human))
|
20 |
+
history_langchain_format.append(AIMessage(content=ai))
|
21 |
+
history_langchain_format.append(HumanMessage(content=message))
|
22 |
+
|
23 |
+
try:
|
24 |
+
gpt_response = chat(history_langchain_format)
|
25 |
+
return gpt_response.content
|
26 |
+
except Exception as e:
|
27 |
+
print(f"An error occurred: {e}")
|
28 |
+
return "Sorry, I couldn't process that message."
|
29 |
+
|
30 |
+
gr.Interface(predict, inputs=["text", "state"], outputs="text").launch()
|