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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -27,13 +27,21 @@ def count_tokens(text: str, model: str) -> int:
27
  enc = _get_encoding(model)
28
  return len(enc.encode(text))
29
 
30
- # Read uploaded file
31
  def read_file_content(file_obj):
32
  if not file_obj:
33
  return ""
34
  try:
35
- content = file_obj.read().decode('utf-8')
36
- return f"\n\n--- Start of file: {file_obj.name} ---\n{content}\n--- End of file ---\n"
 
 
 
 
 
 
 
 
37
  except Exception:
38
  return ""
39
 
@@ -57,18 +65,22 @@ def respond(message, history, model_name, file_obj):
57
  completion_tokens = count_tokens(reply, model_name)
58
  usage_info = f"(Tokens used: prompt={prompt_tokens}, completion={completion_tokens})"
59
 
60
- # Update history
61
- history.append((message, f"{reply}\n\n{usage_info}"))
 
62
  return history
63
 
64
  # Build Gradio interface
65
  with gr.Blocks() as demo:
66
  model_dropdown = gr.Dropdown(ALL_MODELS, value=ALL_MODELS[0], label="Select Model")
67
- chatbot = gr.Chatbot(label="Chat with AI")
68
- file_upload = gr.File(label="Upload File", file_types=[".txt", ".md", ".py"], type="file")
69
  user_input = gr.Textbox(placeholder="Type your message...", show_label=False)
70
 
71
  # Link interactions
72
- user_input.submit(respond, inputs=[user_input, chatbot, model_dropdown, file_upload], outputs=chatbot)
 
 
73
  user_input.submit(lambda: "", None, user_input)
 
74
  demo.launch()
 
27
  enc = _get_encoding(model)
28
  return len(enc.encode(text))
29
 
30
+ # Read uploaded file content (filepath or file-like)
31
  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"
45
  except Exception:
46
  return ""
47
 
 
65
  completion_tokens = count_tokens(reply, model_name)
66
  usage_info = f"(Tokens used: prompt={prompt_tokens}, completion={completion_tokens})"
67
 
68
+ # Update history (OpenAI-style messages)
69
+ history.append({"role": "user", "content": message})
70
+ history.append({"role": "assistant", "content": f"{reply}\n\n{usage_info}"})
71
  return history
72
 
73
  # Build Gradio interface
74
  with gr.Blocks() as demo:
75
  model_dropdown = gr.Dropdown(ALL_MODELS, value=ALL_MODELS[0], label="Select Model")
76
+ chatbot = gr.Chatbot(label="Chat with AI", type="messages")
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)
84
  user_input.submit(lambda: "", None, user_input)
85
+
86
  demo.launch()