Spaces:
Sleeping
Sleeping
Update app.py
Browse files1233456789789101112121314151616 unmodified lines333435353637383940414142433 unmodified lines4748495150535152555354575556575816 unmodified lines75767780787980import osimport functoolsimport openaifrom openai import OpenAIimport tiktokenimport gradio as gr
# Configure API keyopenai.api_key = os.getenv("OPENAI_API_KEY")if not openai.api_key:# Configure API clientapi_key = os.getenv("OPENAI_API_KEY")if not api_key: raise ValueError("Please set the OPENAI_API_KEY environment variable.")client = OpenAI(api_key=api_key)
# Available models (comma-separated in env or fallback list)# Available models (comma-separated in env or fallback)_env_models = os.getenv("OPENAI_MODEL_LIST", "gpt-3.5-turbo,gpt-4")ALL_MODELS = [m.strip() for m in _env_models.split(",") if m.strip()]if not ALL_MODELS:16 unmodified lines if not file_obj: return "" try: # If filepath, open and read if isinstance(file_obj, str): with open(file_obj, 'r', encoding='utf-8') as f: content = f.read() name = os.path.basename(file_obj) else: # file_obj is a file-like object name = getattr(file_obj, 'name', 'uploaded_file') content = file_obj.read().decode('utf-8') return f"\n\n--- Start of file: {name} ---\n{content}\n--- End of file ---\n"3 unmodified lines# Chat response functiondef respond(message, history, model_name, file_obj): history = history or [] # Attach file content if uploaded file_text = read_file_content(file_obj) full_input = message + file_text full_input = (message or "") + file_text
# Send to OpenAI # Call OpenAI chat completion messages = [{"role": "user", "content": full_input}] resp = openai.ChatCompletion.create( resp = client.chat.completions.create( model=model_name, messages=messages )16 unmodified lines file_upload = gr.File(label="Upload File", file_types=[".txt", ".md", ".py"], type="filepath") user_input = gr.Textbox(placeholder="Type your message...", show_label=False)
# Link interactions user_input.submit(respond, inputs=[user_input, chatbot, model_dropdown, file_upload], outputs=chatbot)app.py+9-12