Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516 unmodified lines323334353637383940414243443 unmodified lines4849505152535455565758596016 unmodified lines77787980818283import osimport functoolsimport openaiimport tiktokenimport gradio as gr# Configure API keyopenai.api_key = os.getenv("OPENAI_API_KEY")if not openai.api_key:raise ValueError("Please set the OPENAI_API_KEY environment variable.")# Available models (comma-separated in env or fallback list)_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 linesif not file_obj:return ""try:# If filepath, open and readif 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 objectname = 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 uploadedfile_text = read_file_content(file_obj)full_input = message + file_text# Send to OpenAImessages = [{"role": "user", "content": full_input}]resp = openai.ChatCompletion.create(model=model_name,messages=messages)16 unmodified linesfile_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 interactionsuser_input.submit(respond,inputs=[user_input, chatbot, model_dropdown, file_upload],outputs=chatbot)1234567891011121314151616 unmodified lines33343536373839404142433 unmodified lines47484950515253545556575816 unmodified lines757677787980import osimport functoolsfrom openai import OpenAIimport tiktokenimport gradio as gr# 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)_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 linesif not file_obj:return ""try: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: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 []file_text = read_file_content(file_obj)full_input = (message or "") + file_text# Call OpenAI chat completionmessages = [{"role": "user", "content": full_input}]resp = client.chat.completions.create(model=model_name,messages=messages)16 unmodified linesfile_upload = gr.File(label="Upload File", file_types=[".txt", ".md", ".py"], type="filepath")user_input = gr.Textbox(placeholder="Type your message...", show_label=False)user_input.submit(respond,inputs=[user_input, chatbot, model_dropdown, file_upload],outputs=chatbot)
app.py+9-12