Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,4 +7,69 @@ from openai import OpenAI
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
load_dotenv()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
client = OpenAI(api_key = os.environ.get("OPENAI_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
+
# Get the api key from the environment variable
|
| 11 |
+
open_ai_api_key = os.environ.get("OPENAI_API_KEY")
|
| 12 |
+
if not open_ai_api_key:
|
| 13 |
+
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
| 14 |
+
|
| 15 |
+
#intialize openai client
|
| 16 |
client = OpenAI(api_key = os.environ.get("OPENAI_API_KEY"))
|
| 17 |
+
|
| 18 |
+
System_msg = "act as an experienced blockchain developer,you have been working in this field from the past 15 years.help me understand some concepts, assume i am a complete begineer"
|
| 19 |
+
|
| 20 |
+
ipAddress = None
|
| 21 |
+
|
| 22 |
+
def nowInISt():
|
| 23 |
+
return dt.datetime.now(pytz.timezone("Asia/Kolkata"))
|
| 24 |
+
|
| 25 |
+
def attachIp():
|
| 26 |
+
global ipAddress
|
| 27 |
+
x_forwarded_for = request.headers.get("x-forwarded-for")
|
| 28 |
+
if x_forwarded_for:
|
| 29 |
+
ipAddress = x_forwarded_for
|
| 30 |
+
|
| 31 |
+
def pprint(log: str):
|
| 32 |
+
now = nowInISt()
|
| 33 |
+
now = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 34 |
+
print(f"[{now}] [{ipAddress}] {log}")
|
| 35 |
+
|
| 36 |
+
def predict(message,history):
|
| 37 |
+
history_list = [{"role": "system", "content": System_msg}]
|
| 38 |
+
for human,ai in history:
|
| 39 |
+
history_list.append({"role": "user", "content": human})
|
| 40 |
+
history_list.append({"role": "assistant", "content": ai})
|
| 41 |
+
history_list.append({"role": "user", "content": message})
|
| 42 |
+
|
| 43 |
+
response = client.chat.completions.create(
|
| 44 |
+
model = "gpt-4o-mini",
|
| 45 |
+
message = history_list,
|
| 46 |
+
temperature = 1.0,
|
| 47 |
+
max_tokens=4000,
|
| 48 |
+
stream = True
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
partialMessage = ""
|
| 52 |
+
chunkCount = 0
|
| 53 |
+
for chunk in response:
|
| 54 |
+
chunkContent = chunk.choices[0].delta.content
|
| 55 |
+
if chunkContent:
|
| 56 |
+
chunkCount+=1
|
| 57 |
+
partialMessage= partialMessage + chunkContent
|
| 58 |
+
yield partialMessage
|
| 59 |
+
|
| 60 |
+
pprint(f"[tokens = {chunkCount}] {message}")
|
| 61 |
+
|
| 62 |
+
with gr.interface(
|
| 63 |
+
predict,
|
| 64 |
+
title = "blockchain teacher",
|
| 65 |
+
theme = gr.themes.Soft(),
|
| 66 |
+
chatbot = gr.Chatbot(label ="learn about blochchain technology")
|
| 67 |
+
textbox = gr.Textbox(
|
| 68 |
+
placeholder = "ask me anything about blochchain",
|
| 69 |
+
scale = 7
|
| 70 |
+
max_lines = 2,
|
| 71 |
+
)
|
| 72 |
+
) as demo:
|
| 73 |
+
demo.load(attachIp,None,None)
|
| 74 |
+
|
| 75 |
+
demo.launch()
|