Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,54 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history:
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
):
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
for
|
| 21 |
-
if
|
| 22 |
-
messages.append({"role": "user", "content":
|
| 23 |
-
if
|
| 24 |
-
messages.append({"role": "assistant", "content":
|
| 25 |
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
|
|
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
messages,
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
|
|
|
| 36 |
):
|
| 37 |
-
token =
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a
|
| 50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
gr.Slider(
|
|
@@ -61,4 +63,4 @@ demo = gr.ChatInterface(
|
|
| 61 |
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from typing import List, Tuple
|
| 5 |
|
| 6 |
+
ENDPOINT_URL = "https://api.hyperbolic.xyz/v1"
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
OAI_API_KEY = os.getenv('HYPERBOLIC_XYZ_KEY')
|
| 9 |
+
|
| 10 |
+
client = OpenAI(base_url=ENDPOINT_URL,api_key=OAI_API_KEY)
|
| 11 |
|
| 12 |
def respond(
|
| 13 |
+
message: str,
|
| 14 |
+
history: List[Tuple[str, str]],
|
| 15 |
+
system_message: str,
|
| 16 |
+
max_tokens: int,
|
| 17 |
+
temperature: float,
|
| 18 |
+
top_p: float,
|
| 19 |
):
|
| 20 |
+
# Prepare the conversation history
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
|
| 23 |
+
for user_msg, assistant_msg in history:
|
| 24 |
+
if user_msg:
|
| 25 |
+
messages.append({"role": "user", "content": user_msg})
|
| 26 |
+
if assistant_msg:
|
| 27 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 28 |
|
| 29 |
+
# Add the latest user message
|
| 30 |
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
+
# Stream the response from OpenAI
|
| 33 |
response = ""
|
| 34 |
+
for chunk in client.chat.completions.create(
|
| 35 |
+
model="gpt-4", # or "gpt-3.5-turbo"
|
| 36 |
+
messages=messages,
|
| 37 |
max_tokens=max_tokens,
|
|
|
|
| 38 |
temperature=temperature,
|
| 39 |
top_p=top_p,
|
| 40 |
+
stream=True,
|
| 41 |
):
|
| 42 |
+
token = chunk.choices[0].delta.content or ""
|
|
|
|
| 43 |
response += token
|
| 44 |
yield response
|
| 45 |
|
| 46 |
|
| 47 |
+
# Gradio ChatInterface with additional inputs for customization
|
|
|
|
|
|
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
respond,
|
| 50 |
additional_inputs=[
|
| 51 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
| 52 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 53 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 54 |
gr.Slider(
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
+
demo.launch()
|