resumesearch commited on
Commit
762ce3f
·
verified ·
1 Parent(s): 0a4832a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -1,15 +1,16 @@
1
  import os
2
  import functools
3
- import openai
4
  import tiktoken
5
  import gradio as gr
6
 
7
- # Configure API key
8
- openai.api_key = os.getenv("OPENAI_API_KEY")
9
- if not openai.api_key:
10
  raise ValueError("Please set the OPENAI_API_KEY environment variable.")
 
11
 
12
- # Available models (comma-separated in env or fallback list)
13
  _env_models = os.getenv("OPENAI_MODEL_LIST", "gpt-3.5-turbo,gpt-4")
14
  ALL_MODELS = [m.strip() for m in _env_models.split(",") if m.strip()]
15
  if not ALL_MODELS:
@@ -32,13 +33,11 @@ def read_file_content(file_obj):
32
  if not file_obj:
33
  return ""
34
  try:
35
- # If filepath, open and read
36
  if isinstance(file_obj, str):
37
  with open(file_obj, 'r', encoding='utf-8') as f:
38
  content = f.read()
39
  name = os.path.basename(file_obj)
40
  else:
41
- # file_obj is a file-like object
42
  name = getattr(file_obj, 'name', 'uploaded_file')
43
  content = file_obj.read().decode('utf-8')
44
  return f"\n\n--- Start of file: {name} ---\n{content}\n--- End of file ---\n"
@@ -48,13 +47,12 @@ def read_file_content(file_obj):
48
  # Chat response function
49
  def respond(message, history, model_name, file_obj):
50
  history = history or []
51
- # Attach file content if uploaded
52
  file_text = read_file_content(file_obj)
53
- full_input = message + file_text
54
 
55
- # Send to OpenAI
56
  messages = [{"role": "user", "content": full_input}]
57
- resp = openai.ChatCompletion.create(
58
  model=model_name,
59
  messages=messages
60
  )
@@ -77,7 +75,6 @@ with gr.Blocks() as demo:
77
  file_upload = gr.File(label="Upload File", file_types=[".txt", ".md", ".py"], type="filepath")
78
  user_input = gr.Textbox(placeholder="Type your message...", show_label=False)
79
 
80
- # Link interactions
81
  user_input.submit(respond,
82
  inputs=[user_input, chatbot, model_dropdown, file_upload],
83
  outputs=chatbot)
 
1
  import os
2
  import functools
3
+ from openai import OpenAI
4
  import tiktoken
5
  import gradio as gr
6
 
7
+ # Configure API client
8
+ api_key = os.getenv("OPENAI_API_KEY")
9
+ if not api_key:
10
  raise ValueError("Please set the OPENAI_API_KEY environment variable.")
11
+ client = OpenAI(api_key=api_key)
12
 
13
+ # Available models (comma-separated in env or fallback)
14
  _env_models = os.getenv("OPENAI_MODEL_LIST", "gpt-3.5-turbo,gpt-4")
15
  ALL_MODELS = [m.strip() for m in _env_models.split(",") if m.strip()]
16
  if not ALL_MODELS:
 
33
  if not file_obj:
34
  return ""
35
  try:
 
36
  if isinstance(file_obj, str):
37
  with open(file_obj, 'r', encoding='utf-8') as f:
38
  content = f.read()
39
  name = os.path.basename(file_obj)
40
  else:
 
41
  name = getattr(file_obj, 'name', 'uploaded_file')
42
  content = file_obj.read().decode('utf-8')
43
  return f"\n\n--- Start of file: {name} ---\n{content}\n--- End of file ---\n"
 
47
  # Chat response function
48
  def respond(message, history, model_name, file_obj):
49
  history = history or []
 
50
  file_text = read_file_content(file_obj)
51
+ full_input = (message or "") + file_text
52
 
53
+ # Call OpenAI chat completion
54
  messages = [{"role": "user", "content": full_input}]
55
+ resp = client.chat.completions.create(
56
  model=model_name,
57
  messages=messages
58
  )
 
75
  file_upload = gr.File(label="Upload File", file_types=[".txt", ".md", ".py"], type="filepath")
76
  user_input = gr.Textbox(placeholder="Type your message...", show_label=False)
77
 
 
78
  user_input.submit(respond,
79
  inputs=[user_input, chatbot, model_dropdown, file_upload],
80
  outputs=chatbot)