Spaces:
Sleeping
Sleeping
Commit
·
1fd511b
1
Parent(s):
990e595
feat(): initial commit
Browse files- README.md +2 -2
- app.py +69 -0
- chat_history.txt +0 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
---
|
2 |
title: Credogpt
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.29.0
|
|
|
1 |
---
|
2 |
title: Credogpt
|
3 |
+
emoji: 👾
|
4 |
+
colorFrom: pink
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.29.0
|
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
|
5 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
6 |
+
MODEL_NAME = "gpt-4"
|
7 |
+
|
8 |
+
# Function to send messages to GPT-4
|
9 |
+
global_chat_history = ""
|
10 |
+
|
11 |
+
def generate_response(prompt):
|
12 |
+
global global_chat_history
|
13 |
+
# Send message to GPT-4
|
14 |
+
message = {"role": "user", "content": prompt}
|
15 |
+
chat_history_messages = [{"role": "user", "content": m} for m in global_chat_history.split("\n")] + [message]
|
16 |
+
response = openai.ChatCompletion.create(
|
17 |
+
model=MODEL_NAME,
|
18 |
+
messages=chat_history_messages,
|
19 |
+
temperature=1,
|
20 |
+
max_tokens=1024,
|
21 |
+
n=1,
|
22 |
+
stop=None,
|
23 |
+
)
|
24 |
+
# Update global chat history with the new message and response
|
25 |
+
global_chat_history += f"\nUser: {prompt}\nAI: {response.choices[0].message.content}"
|
26 |
+
return response.choices[0].message.content
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
blocks = gr.Interface(
|
33 |
+
fn=generate_response,
|
34 |
+
inputs=[gr.Textbox(placeholder="Enter your prompt...", lines=2)],
|
35 |
+
outputs=[gr.Textbox(placeholder="Response", lines=10, readonly=True)],
|
36 |
+
title="GPT-4 Chatbot",
|
37 |
+
theme="compact",
|
38 |
+
layout="vertical",
|
39 |
+
allow_flagging=False,
|
40 |
+
)
|
41 |
+
|
42 |
+
|
43 |
+
def add_cors_headers(response):
|
44 |
+
response.headers["Access-Control-Allow-Origin"] = "*"
|
45 |
+
response.headers["Access-Control-Allow-Headers"] = "Content-Type,Authorization"
|
46 |
+
response.headers["Access-Control-Allow-Methods"] = "POST,OPTIONS"
|
47 |
+
return response
|
48 |
+
|
49 |
+
|
50 |
+
# @blocks.flask_app.after_request
|
51 |
+
def enable_cors(response):
|
52 |
+
return add_cors_headers(response)
|
53 |
+
|
54 |
+
|
55 |
+
def save_conversation():
|
56 |
+
global global_chat_history
|
57 |
+
if global_chat_history is None:
|
58 |
+
return ""
|
59 |
+
with open('chat_history.txt', 'a') as f:
|
60 |
+
f.write(global_chat_history)
|
61 |
+
f.write("\n")
|
62 |
+
return ""
|
63 |
+
|
64 |
+
|
65 |
+
with blocks:
|
66 |
+
b1 = gr.Button("Save Conversation")
|
67 |
+
b1.click(save_conversation)
|
68 |
+
|
69 |
+
blocks.launch()
|
chat_history.txt
ADDED
File without changes
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio==3.29.0
|
2 |
+
openai==0.27.2
|