Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Goal: Build an AI powered chat bot
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
client = OpenAI()
|
| 11 |
+
|
| 12 |
+
def save_history(history):
|
| 13 |
+
with open("data.json", "w") as data:
|
| 14 |
+
json.dump(history, data)
|
| 15 |
+
|
| 16 |
+
def load_history():
|
| 17 |
+
with open("data.json", "r") as data:
|
| 18 |
+
return json.load(data)
|
| 19 |
+
|
| 20 |
+
# Backend: Python
|
| 21 |
+
def echo(message, history):
|
| 22 |
+
# LLM: OpenAI
|
| 23 |
+
converstation_history = load_history()
|
| 24 |
+
|
| 25 |
+
converstation_history.append({"role": "user", "content": message})
|
| 26 |
+
|
| 27 |
+
completion = client.chat.completions.create(
|
| 28 |
+
model="gpt-4o-mini",
|
| 29 |
+
messages=converstation_history
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
converstation_history.append({"role": "assistant", "content": completion.choices[0].message.content})
|
| 33 |
+
|
| 34 |
+
save_history(converstation_history)
|
| 35 |
+
|
| 36 |
+
return completion.choices[0].message.content
|
| 37 |
+
|
| 38 |
+
# Frontend: Gradio
|
| 39 |
+
demo = gr.ChatInterface(fn=echo, type="messages", examples=["I want to lear about LLMs", "What is NLP", "what is RAG"], title="LLM Mentor")
|
| 40 |
+
demo.launch()
|