admin08077 commited on
Commit
f883ad2
·
verified ·
1 Parent(s): 8639f04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -111
app.py CHANGED
@@ -2,90 +2,61 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
  import nltk
5
- import json
6
- import io
7
- import base64
8
- from fpdf import FPDF
9
- from textblob import TextBlob
10
  import PyPDF2
11
- import tempfile
12
 
13
- # Download the NLTK punkt tokenizer if not already present
14
  nltk.download("punkt", quiet=True)
15
 
16
  ###############################################################################
17
  # Hugging Face Chat Code #
18
  ###############################################################################
19
- """
20
- For more information on Hugging Face Inference API support, please check:
21
- https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
22
- """
23
 
24
- # Initialize your Hugging Face model client
25
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
26
 
27
- def respond(
28
- message,
29
- history: list[tuple[str, str]],
30
- system_message,
31
- max_tokens,
32
- temperature,
33
- top_p,
34
- file_content
35
- ):
36
  """
37
- Streams the chat response from the Hugging Face model.
38
- The uploaded file's content is appended to the system message for context.
39
- Yields tokens as they arrive.
40
  """
41
- # Append file content to the system prompt if available.
42
  if file_content and file_content.strip():
43
- system_message = system_message + "\n\nFile content:\n" + file_content
44
-
45
- # Build the messages list to send
46
  messages = [{"role": "system", "content": system_message}]
47
- for user_msg, assistant_msg in history:
48
- if user_msg:
49
- messages.append({"role": "user", "content": user_msg})
50
- if assistant_msg:
51
- messages.append({"role": "assistant", "content": assistant_msg})
52
  messages.append({"role": "user", "content": message})
53
-
54
- response = ""
55
  try:
56
- for partial in client.chat_completion(
57
  messages,
58
  max_tokens=max_tokens,
59
- stream=True,
60
  temperature=temperature,
61
  top_p=top_p,
62
- ):
63
- token = partial.choices[0].delta.get("content", "")
64
- response += token
65
- yield response
66
  except Exception as e:
67
- yield f"Error during model response: {e}"
68
 
69
  ###############################################################################
70
- # File Upload & Parsing Functionality #
71
  ###############################################################################
72
 
73
  def parse_file(file_obj):
74
  """
75
- Parses the provided file object.
76
- For PDFs, it extracts text using PyPDF2.
77
- For other file types, it attempts to decode as UTF-8 text.
78
  """
79
- # Expect file_obj to be a file-like object with a 'name' attribute.
80
  file_extension = file_obj.name.split('.')[-1].lower()
81
  if file_extension == "pdf":
82
  try:
83
  reader = PyPDF2.PdfReader(file_obj)
84
- text = ""
85
- for page in reader.pages:
86
- extracted = page.extract_text() or ""
87
- text += extracted + "\n"
88
- return text
89
  except Exception as e:
90
  return f"Error reading PDF: {e}"
91
  else:
@@ -96,83 +67,66 @@ def parse_file(file_obj):
96
 
97
  def load_files(files):
98
  """
99
- Processes a list of file paths (uploaded with type="filepath").
100
- Opens each file, extracts its text, and concatenates the results.
101
  """
102
- all_text = ""
103
- for file_path in files:
104
  try:
105
- with open(file_path, "rb") as f:
106
  content = parse_file(f)
107
- all_text += content + "\n"
108
  except Exception as e:
109
- all_text += f"Error processing file {file_path}: {e}\n"
110
- return all_text
111
 
112
  ###############################################################################
113
- # Gradio UI Layout #
114
  ###############################################################################
115
 
116
  with gr.Blocks() as demo:
117
- gr.Markdown("# **Combined Chat & File Upload App**")
118
  gr.Markdown(
119
  """
120
- This app allows you to upload file(s) and chat with an AI assistant that uses
121
- the uploaded file(s) as context throughout the conversation.
122
-
123
- - **Upload File(s):** (PDF or TXT files) The content is automatically parsed.
124
- - **Chat:** Your messages are sent to the AI along with the uploaded file's context.
125
- """
126
  )
127
 
128
- # State to hold the concatenated file content and conversation history.
129
  file_content_state = gr.State("")
130
- chat_history_state = gr.State([]) # List of tuples: (user_message, assistant_response)
131
-
132
- # File uploader (using type="filepath" so we get the file paths)
133
  file_input = gr.File(label="Upload File(s)", file_count="multiple", type="filepath")
134
- # When the file(s) are uploaded, automatically load them and store the combined text.
135
  file_input.change(fn=load_files, inputs=file_input, outputs=file_content_state)
136
-
137
- gr.Markdown("## Chat with AI (File context is automatically used)")
138
- chatbot = gr.Chatbot(label="Chat History")
139
- user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=2)
140
-
141
- def handle_user_message(user_message, chat_history, file_content):
 
 
 
 
 
 
 
 
142
  if not user_message.strip():
143
- return "", chat_history
144
- # Append the user's message to chat history.
145
- chat_history.append((user_message, None))
146
- # Generate the AI response using the uploaded file content as context.
147
- response_gen = respond(
148
- message=user_message,
149
- history=chat_history,
150
- system_message="You are a helpful AI assistant.",
151
- max_tokens=512,
152
- temperature=0.7,
153
- top_p=0.95,
154
- file_content=file_content
155
- )
156
- response = ""
157
- try:
158
- for partial in response_gen:
159
- response = partial
160
- # Update the last chat history entry with the current response.
161
- chat_history[-1] = (user_message, response)
162
- yield "", chat_history
163
- except Exception as e:
164
- chat_history[-1] = (user_message, f"Error: {e}")
165
- yield "", chat_history
166
 
167
- # When a message is submitted (by pressing Enter), handle the message.
168
  user_input.submit(
169
- handle_user_message,
170
- inputs=[user_input, chat_history_state, file_content_state],
171
- outputs=[user_input, chatbot],
172
- queue=True
173
  )
174
-
175
  demo.launch(server_name="0.0.0.0", server_port=7860)
176
-
177
- if __name__ == "__main__":
178
- demo.launch()
 
2
  from huggingface_hub import InferenceClient
3
 
4
  import nltk
 
 
 
 
 
5
  import PyPDF2
 
6
 
 
7
  nltk.download("punkt", quiet=True)
8
 
9
  ###############################################################################
10
  # Hugging Face Chat Code #
11
  ###############################################################################
 
 
 
 
12
 
13
+ # Initialize the Hugging Face model client
14
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
15
 
16
+ def respond(message, history, system_message, max_tokens, temperature, top_p, file_content):
 
 
 
 
 
 
 
 
17
  """
18
+ Calls the Hugging Face model for a response.
19
+ Includes file content in the system message for context.
 
20
  """
21
+ # Append file content to system message if available
22
  if file_content and file_content.strip():
23
+ system_message += f"\n\nFile content:\n{file_content}"
24
+
25
+ # Prepare the message payload
26
  messages = [{"role": "system", "content": system_message}]
27
+ for user, assistant in history:
28
+ if user:
29
+ messages.append({"role": "user", "content": user})
30
+ if assistant:
31
+ messages.append({"role": "assistant", "content": assistant})
32
  messages.append({"role": "user", "content": message})
33
+
34
+ # Get the model response
35
  try:
36
+ completion = client.chat_completion(
37
  messages,
38
  max_tokens=max_tokens,
 
39
  temperature=temperature,
40
  top_p=top_p,
41
+ )
42
+ return completion.choices[0].message["content"]
 
 
43
  except Exception as e:
44
+ return f"Error: {e}"
45
 
46
  ###############################################################################
47
+ # File Upload & Parsing Functions #
48
  ###############################################################################
49
 
50
  def parse_file(file_obj):
51
  """
52
+ Parses uploaded files and extracts content.
53
+ Supports PDFs and plain text.
 
54
  """
 
55
  file_extension = file_obj.name.split('.')[-1].lower()
56
  if file_extension == "pdf":
57
  try:
58
  reader = PyPDF2.PdfReader(file_obj)
59
+ return "\n".join(page.extract_text() or "" for page in reader.pages)
 
 
 
 
60
  except Exception as e:
61
  return f"Error reading PDF: {e}"
62
  else:
 
67
 
68
  def load_files(files):
69
  """
70
+ Loads multiple files, parses their content, and concatenates the text.
 
71
  """
72
+ combined_text = ""
73
+ for file in files:
74
  try:
75
+ with open(file, "rb") as f:
76
  content = parse_file(f)
77
+ combined_text += content + "\n"
78
  except Exception as e:
79
+ combined_text += f"Error processing file {file}: {e}\n"
80
+ return combined_text
81
 
82
  ###############################################################################
83
+ # Gradio UI Layout #
84
  ###############################################################################
85
 
86
  with gr.Blocks() as demo:
87
+ gr.Markdown("# **Chat with File Context**")
88
  gr.Markdown(
89
  """
90
+ This app lets you upload file(s) and chat with an AI assistant.
91
+ Uploaded file content will provide context for the conversation.
92
+ """
 
 
 
93
  )
94
 
95
+ # States to store file content and chat history
96
  file_content_state = gr.State("")
97
+ chat_history_state = gr.State([])
98
+
99
+ # File Upload Section
100
  file_input = gr.File(label="Upload File(s)", file_count="multiple", type="filepath")
 
101
  file_input.change(fn=load_files, inputs=file_input, outputs=file_content_state)
102
+
103
+ # Chat Section
104
+ gr.Markdown("## Chat")
105
+ chatbot = gr.Chatbot(label="Conversation")
106
+ user_input = gr.Textbox(label="Your Message", placeholder="Ask something...", lines=2)
107
+
108
+ # Model Configuration Sliders
109
+ system_prompt = gr.Textbox(label="System Prompt", value="You are a helpful AI assistant.", interactive=True)
110
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens")
111
+ temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
112
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
113
+
114
+ # Chat Function
115
+ def chat_function(user_message, history, file_content, system_prompt, max_tokens, temperature, top_p):
116
  if not user_message.strip():
117
+ return "", history
118
+ # Append user's message to the chat history
119
+ history.append((user_message, ""))
120
+ # Get response from the model
121
+ assistant_response = respond(user_message, history, system_prompt, max_tokens, temperature, top_p, file_content)
122
+ history[-1] = (user_message, assistant_response)
123
+ return "", history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
+ # Submit Chat Input
126
  user_input.submit(
127
+ fn=chat_function,
128
+ inputs=[user_input, chat_history_state, file_content_state, system_prompt, max_tokens, temperature, top_p],
129
+ outputs=[user_input, chatbot]
 
130
  )
131
+
132
  demo.launch(server_name="0.0.0.0", server_port=7860)