Spaces:
Sleeping
Sleeping
Download app.py from MichaelChengERB/AzureOpenAIDemo4o: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/MichaelChengERB/AzureOpenAIDemo4o/resolve/main/app.py
- Command line
-
hf download hf://spaces/MichaelChengERB/AzureOpenAIDemo4o/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MichaelChengERB/AzureOpenAIDemo4o/resolve/main/app.py
1.52 kB
| import gradio as gr | |
| import os | |
| import requests | |
| import json | |
| subscription_key = os.getenv("key") | |
| baseEndPoint = os.getenv("endpoint") | |
| deployment = os.getenv("deployment") | |
| endpoint = f"{baseEndPoint}openai/deployments/{deployment}/chat/completions?api-version=2024-02-01" | |
| headers = { | |
| "Content-Type": "application/json", | |
| "api-key": subscription_key | |
| } | |
| def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p,): | |
| body = { | |
| "messages": [ | |
| { # System Message | |
| "role": "system", | |
| "content": system_message | |
| }, | |
| { # User Prompt | |
| "role": "user", | |
| "content": message | |
| } | |
| ], | |
| "temperature": temperature, | |
| "top_p": top_p, | |
| "max_tokens": max_tokens, | |
| "stop": "" | |
| } | |
| response = requests.post( | |
| endpoint, | |
| headers=headers, | |
| json=body | |
| ) | |
| yield response.json()["choices"][0]["message"]["content"] | |
| demo = gr.ChatInterface( | |
| respond, | |
| additional_inputs=[ | |
| gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
| gr.Slider(minimum=1, maximum=800, value=200, step=1, label="Max new tokens"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider( | |
| minimum=0.1, | |
| maximum=1.0, | |
| value=0.95, | |
| step=0.05, | |
| label="Top-p (nucleus sampling)", | |
| ), | |
| ], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |