Spaces:
Sleeping
Sleeping
starting with existing framework, adding new parameters and modernizing a bit
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
+
|
| 7 |
+
print("Access token loaded.")
|
| 8 |
+
|
| 9 |
+
client = OpenAI(
|
| 10 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
| 11 |
+
api_key=ACCESS_TOKEN,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
print("OpenAI client initialized.")
|
| 15 |
+
|
| 16 |
+
def respond(
|
| 17 |
+
message,
|
| 18 |
+
history: list[tuple[str, str]],
|
| 19 |
+
system_message,
|
| 20 |
+
max_tokens,
|
| 21 |
+
temperature,
|
| 22 |
+
top_p,
|
| 23 |
+
):
|
| 24 |
+
print(f"Received message: {message}")
|
| 25 |
+
print(f"History: {history}")
|
| 26 |
+
print(f"System message: {system_message}")
|
| 27 |
+
print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
|
| 28 |
+
|
| 29 |
+
messages = [{"role": "system", "content": system_message}]
|
| 30 |
+
|
| 31 |
+
for val in history:
|
| 32 |
+
if val[0]:
|
| 33 |
+
messages.append({"role": "user", "content": val[0]})
|
| 34 |
+
print(f"Added user message to context: {val[0]}")
|
| 35 |
+
if val[1]:
|
| 36 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 37 |
+
print(f"Added assistant message to context: {val[1]}")
|
| 38 |
+
|
| 39 |
+
messages.append({"role": "user", "content": message})
|
| 40 |
+
|
| 41 |
+
response = ""
|
| 42 |
+
print("Sending request to OpenAI API.")
|
| 43 |
+
|
| 44 |
+
for message in client.chat.completions.create(
|
| 45 |
+
model="meta-llama/Llama-3.3-70B-Instruct",
|
| 46 |
+
max_tokens=max_tokens,
|
| 47 |
+
stream=True,
|
| 48 |
+
temperature=temperature,
|
| 49 |
+
top_p=top_p,
|
| 50 |
+
messages=messages,
|
| 51 |
+
):
|
| 52 |
+
token = message.choices[0].delta.content
|
| 53 |
+
print(f"Received token: {token}")
|
| 54 |
+
response += token
|
| 55 |
+
yield response
|
| 56 |
+
|
| 57 |
+
print("Completed response generation.")
|
| 58 |
+
|
| 59 |
+
chatbot = gr.Chatbot(height=600)
|
| 60 |
+
|
| 61 |
+
print("Chatbot interface created.")
|
| 62 |
+
|
| 63 |
+
demo = gr.ChatInterface(
|
| 64 |
+
respond,
|
| 65 |
+
additional_inputs=[
|
| 66 |
+
gr.Textbox(value="", label="System message"),
|
| 67 |
+
gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
|
| 68 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 69 |
+
gr.Slider(
|
| 70 |
+
minimum=0.1,
|
| 71 |
+
maximum=1.0,
|
| 72 |
+
value=0.95,
|
| 73 |
+
step=0.05,
|
| 74 |
+
label="Top-P",
|
| 75 |
+
),
|
| 76 |
+
|
| 77 |
+
],
|
| 78 |
+
fill_height=True,
|
| 79 |
+
chatbot=chatbot,
|
| 80 |
+
theme="Nymbo/Nymbo_Theme",
|
| 81 |
+
)
|
| 82 |
+
print("Gradio interface initialized.")
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
print("Launching the demo application.")
|
| 86 |
+
demo.launch()
|