Download app.py from jageet/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/spaces/jageet/chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/jageet/chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/jageet/chatbot/resolve/main/app.py
1.83 kB
| import gradio as gr | |
| import openai | |
| # Set your OpenAI API key here | |
| openai.api_key = "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" | |
| def respond( | |
| message, | |
| history: list[tuple[str, str]], | |
| system_message, | |
| max_tokens, | |
| temperature, | |
| top_p, | |
| ): | |
| # Prepare the messages for the OpenAI API | |
| messages = [{"role": "system", "content": system_message}] | |
| for val in history: | |
| if val[0]: | |
| messages.append({"role": "user", "content": val[0]}) | |
| if val[1]: | |
| messages.append({"role": "assistant", "content": val[1]}) | |
| messages.append({"role": "user", "content": message}) | |
| # Call OpenAI's ChatCompletion API | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o", # You can replace with "gpt-4" if available | |
| messages=messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| stream=True # Stream the response token by token | |
| ) | |
| # Generate the response in real-time | |
| chat_response = "" | |
| for chunk in response: | |
| token = chunk.choices[0].delta.get('content', '') | |
| chat_response += token | |
| yield chat_response | |
| # Set up the Gradio interface | |
| demo = gr.ChatInterface( | |
| respond, | |
| additional_inputs=[ | |
| gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, 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() | |