Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from openai import OpenAI | |
| from dotenv import load_dotenv | |
| import os | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| def chat_with_llm(message, history, temperature=0.7, top_p=1.0, max_tokens=256): | |
| """Function to interact with OpenAI's Chat API with additional controls.""" | |
| # Convert history (list of tuples) into OpenAI's expected format | |
| formatted_history = [] | |
| for user_msg, assistant_msg in history: | |
| formatted_history.append({"role": "user", "content": user_msg}) | |
| formatted_history.append({"role": "assistant", "content": assistant_msg}) | |
| # Make the API call | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[{"role": "system", "content": "You are a helpful assistant."}] + | |
| formatted_history + | |
| [{"role": "user", "content": message}], | |
| temperature=temperature, | |
| top_p=top_p, | |
| max_tokens=max_tokens | |
| ) | |
| return response.choices[0].message.content | |
| # Create Gradio chat interface with sliders for temperature, top_p, and max_tokens | |
| demo = gr.ChatInterface( | |
| fn=chat_with_llm, | |
| additional_inputs=[ | |
| gr.Slider(0, 1, value=0.7, label="Temperature"), | |
| gr.Slider(0, 1, value=1.0, label="Top-p"), | |
| gr.Slider(1, 1024, value=256, label="Max Tokens") | |
| ] | |
| ) | |
| # Enable sharing for external access | |
| demo.launch() | |