Spaces:
Sleeping
Sleeping
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
client = OpenAI()
|
| 8 |
+
|
| 9 |
+
# Backend: Python
|
| 10 |
+
def echo(message, history):
|
| 11 |
+
# Convert Gradio history format to OpenAI messages format
|
| 12 |
+
messages = [
|
| 13 |
+
{"role": "system", "content": "You are a helpful LLM teacher."}
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
# Add chat history
|
| 17 |
+
for user_msg, bot_msg in history:
|
| 18 |
+
messages.append({"role": "user", "content": user_msg})
|
| 19 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 20 |
+
|
| 21 |
+
# Add current message
|
| 22 |
+
messages.append({"role": "user", "content": message})
|
| 23 |
+
|
| 24 |
+
# Get response from OpenAI
|
| 25 |
+
completion = client.chat.completions.create(
|
| 26 |
+
model="gpt-4o-mini",
|
| 27 |
+
messages=messages
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return completion.choices[0].message.content
|
| 31 |
+
|
| 32 |
+
# Frontend: Gradio
|
| 33 |
+
demo = gr.ChatInterface(
|
| 34 |
+
fn=echo,
|
| 35 |
+
examples=["I want to learn about LLMs", "What is NLP", "What is RAG"],
|
| 36 |
+
title="LLM Mentor"
|
| 37 |
+
)
|
| 38 |
+
demo.launch()
|