Download app.py from ZENLLC/mod7: direct link, hf CLI and curl.
- Browser
- Download file 1 kB
-
https://huggingface.co/spaces/ZENLLC/mod7/resolve/main/app.py
- Command line
-
hf download hf://spaces/ZENLLC/mod7/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ZENLLC/mod7/resolve/main/app.py
1 kB
| import gradio as gr | |
| import openai | |
| import os | |
| from dotenv import load_dotenv | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # Get the API key from the environment variable | |
| API_KEY = os.getenv("OPENAI_API_KEY") | |
| openai.api_key = API_KEY | |
| # Function to interact with OpenAI API | |
| def chat_with_model(prompt): | |
| if not API_KEY: | |
| return "Error: API key not found. Please set it in the environment variables." | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4", # Replace with your desired model | |
| messages=[{"role": "user", "content": prompt}] | |
| ) | |
| return response["choices"][0]["message"]["content"] | |
| except openai.error.OpenAIError as e: | |
| return f"Error: {str(e)}" | |
| # Gradio Interface | |
| iface = gr.Interface( | |
| fn=chat_with_model, | |
| inputs="text", | |
| outputs="text", | |
| title="ZEN AI Chatbot", | |
| description="A simple chatbot powered by OpenAI's GPT models." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() |