Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# app.py (for Hugging Face Space & local dev)
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
from langchain_core.prompts import ChatPromptTemplate
|
|
@@ -72,19 +72,24 @@ def chat_fn(user_message, history):
|
|
| 72 |
|
| 73 |
css = ".chat-message.user {color: #0b5394;} .chat-message.bot {color: #38761d;}"
|
| 74 |
|
| 75 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 76 |
-
|
| 77 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 78 |
# history is now a list of {"role":"user"/"assistant","content":...}
|
| 79 |
messages = [{"role":"system","content":system_message}] + history
|
| 80 |
messages.append({"role":"user","content":message})
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
demo = gr.ChatInterface(
|
| 90 |
respond,
|
|
|
|
| 1 |
# app.py (for Hugging Face Space & local dev)
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
+
import openai
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
from langchain_core.prompts import ChatPromptTemplate
|
|
|
|
| 72 |
|
| 73 |
css = ".chat-message.user {color: #0b5394;} .chat-message.bot {color: #38761d;}"
|
| 74 |
|
|
|
|
|
|
|
| 75 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 76 |
# history is now a list of {"role":"user"/"assistant","content":...}
|
| 77 |
messages = [{"role":"system","content":system_message}] + history
|
| 78 |
messages.append({"role":"user","content":message})
|
| 79 |
|
| 80 |
+
for u,b in history:
|
| 81 |
+
msgs += [{"role":"user","content":u}, {"role":"assistant","content":b}]
|
| 82 |
+
msgs.append({"role":"user","content":message})
|
| 83 |
+
|
| 84 |
+
resp = ""
|
| 85 |
+
for chunk in openai.ChatCompletion.create(
|
| 86 |
+
model="gpt-4o-mini", # or gpt-4, gpt-3.5-turbo, etc.
|
| 87 |
+
messages=msgs,
|
| 88 |
+
stream=True
|
| 89 |
+
):
|
| 90 |
+
delta = chunk.choices[0].delta.get("content","")
|
| 91 |
+
resp += delta
|
| 92 |
+
yield resp
|
| 93 |
|
| 94 |
demo = gr.ChatInterface(
|
| 95 |
respond,
|