Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.chat_models import ChatOpenAI
|
| 2 |
+
from langchain.schema import AIMessage, HumanMessage
|
| 3 |
+
import openai
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
os.environ["OPENAI_API_KEY"] = os.getenv(MY_OPENAI_KEY) # Replace with your key
|
| 8 |
+
|
| 9 |
+
llm = ChatOpenAI(temperature=1.0, model='gpt-3.5-turbo-1106')
|
| 10 |
+
|
| 11 |
+
def predict(message, history):
|
| 12 |
+
global history_langchain_format # Create global history list for the chat
|
| 13 |
+
history_langchain_format = []
|
| 14 |
+
for human, ai in history:
|
| 15 |
+
history_langchain_format.append(HumanMessage(content=human))
|
| 16 |
+
history_langchain_format.append(AIMessage(content=ai))
|
| 17 |
+
history_langchain_format.append(HumanMessage(content=message))
|
| 18 |
+
gpt_response = llm(history_langchain_format)
|
| 19 |
+
return gpt_response.content
|
| 20 |
+
|
| 21 |
+
gr.ChatInterface(fn=predict, title= 'Simple Chat Box', description= 'A simple chatbox hosted in HF by Adhya').launch()
|