Spaces:
Runtime error
Runtime error
Mahadih534
commited on
Commit
·
9845207
1
Parent(s):
a6d0595
CustomGPT
Browse files- CustomGPT.py +32 -0
CustomGPT.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
from langchain.chat_models import ChatOpenAI
|
5 |
+
from langchain.schema import AIMessage, HumanMessage
|
6 |
+
|
7 |
+
os.environ["OPENAI_API_KEY"] = "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" # Replace with your key
|
8 |
+
|
9 |
+
llm = ChatOpenAI(temperature=1.0, model='gpt-3.5-turbo-0613')
|
10 |
+
|
11 |
+
def predict(message, history):
|
12 |
+
history_langchain_format = []
|
13 |
+
for human, ai in history:
|
14 |
+
history_langchain_format.append(HumanMessage(content=human))
|
15 |
+
history_langchain_format.append(AIMessage(content=ai))
|
16 |
+
history_langchain_format.append(HumanMessage(content=message))
|
17 |
+
gpt_response = llm(history_langchain_format)
|
18 |
+
return gpt_response.content
|
19 |
+
|
20 |
+
gr.ChatInterface(
|
21 |
+
predict,
|
22 |
+
chatbot=gr.Chatbot(height=300),
|
23 |
+
textbox=gr.Textbox(placeholder="Custom GPT", container=False, scale=7),
|
24 |
+
title="Custom ChatGPT",
|
25 |
+
description="Ask any question",
|
26 |
+
theme="soft",
|
27 |
+
examples=["Hello", "are you GPT-4?", "how can i build my own ChatGPT?"],
|
28 |
+
cache_examples=True,
|
29 |
+
retry_btn=None,
|
30 |
+
undo_btn="Delete Previous",
|
31 |
+
clear_btn="Clear",
|
32 |
+
).launch()
|