Sg-at-srijan-us-kg commited on
Commit
e84f2ca
1 Parent(s): c9b44f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
3
 
4
  client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
@@ -15,16 +16,20 @@ def respond(
15
  # Initialize the messages with the system message
16
  messages = [{"role": "system", "content": system_message}]
17
 
18
- # Read file content if a file is uploaded
19
  if file is not None:
20
  try:
21
- if hasattr(file, 'value'): # Check if file is a NamedString or similar
22
- file_content = file.value.decode("utf-8") # Decode the string content
23
- else:
24
- file_content = file.read().decode("utf-8") # Fallback for other file types
 
 
 
 
 
 
25
 
26
- print("File content:", file_content) # Debug print
27
- message = f"{file_content}\n\n{message}" # Append file content to message
28
  except Exception as e:
29
  print("Error reading file:", e)
30
  message = f"(Error reading file: {e})\n\n{message}"
 
1
  import gradio as gr
2
+ import tempfile
3
  from huggingface_hub import InferenceClient
4
 
5
  client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
 
16
  # Initialize the messages with the system message
17
  messages = [{"role": "system", "content": system_message}]
18
 
19
+ # Handle file upload
20
  if file is not None:
21
  try:
22
+ # Save the uploaded file to a temporary file
23
+ with tempfile.NamedTemporaryFile(delete=False, mode="wb") as temp_file:
24
+ temp_file.write(file.read())
25
+ temp_file_path = temp_file.name # Store the file path
26
+
27
+ # Read the content from the saved file
28
+ with open(temp_file_path, "r", encoding="utf-8") as f:
29
+ file_content = f.read()
30
+ print("File content:", file_content) # Debug print
31
+ message = f"{file_content}\n\n{message}" # Append file content to message
32
 
 
 
33
  except Exception as e:
34
  print("Error reading file:", e)
35
  message = f"(Error reading file: {e})\n\n{message}"