Intellectualtech commited on
Commit
28dad60
·
verified ·
1 Parent(s): 77e115a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -11
app.py CHANGED
@@ -2,6 +2,10 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from typing import List, Tuple
4
  import logging
 
 
 
 
5
 
6
  # Configure logging for better debugging and monitoring
7
  logging.basicConfig(
@@ -18,8 +22,47 @@ except Exception as e:
18
  logger.error(f"Failed to initialize InferenceClient: {str(e)}")
19
  raise
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def respond(
22
  message: str,
 
 
23
  history: List[Tuple[str, str]],
24
  system_message: str,
25
  max_tokens: int,
@@ -27,26 +70,25 @@ def respond(
27
  top_p: float,
28
  ) -> str:
29
  """
30
- Generates an educational response to a student's query using the HuggingFace Inference API.
31
-
32
  Args:
33
  message (str): The student's input question or query.
 
 
34
  history (List[Tuple[str, str]]): Chat history with student and AI teacher messages.
35
  system_message (str): The system prompt defining the AI teacher's behavior.
36
  max_tokens (int): Maximum number of tokens to generate.
37
  temperature (float): Controls randomness in response generation.
38
  top_p (float): Controls diversity via nucleus sampling.
39
-
40
  Yields:
41
  str: The AI teacher's response, streamed token by token.
42
-
43
  Raises:
44
  ValueError: If input parameters are invalid.
45
  RuntimeError: If the API call fails.
46
  """
47
  # Validate input parameters
48
- if not message.strip():
49
- raise ValueError("Input message cannot be empty")
50
  if max_tokens < 1 or max_tokens > 2048:
51
  raise ValueError("max_tokens must be between 1 and 2048")
52
  if temperature < 0.1 or temperature > 2.0:
@@ -54,6 +96,20 @@ def respond(
54
  if top_p < 0.1 or top_p > 1.0:
55
  raise ValueError("top_p must be between 0.1 and 1.0")
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  # Construct the message history
58
  messages = [{"role": "system", "content": system_message}]
59
  for user_msg, assistant_msg in history:
@@ -61,7 +117,7 @@ def respond(
61
  messages.append({"role": "user", "content": user_msg})
62
  if assistant_msg:
63
  messages.append({"role": "assistant", "content": assistant_msg})
64
- messages.append({"role": "user", "content": message})
65
 
66
  response = ""
67
  try:
@@ -81,20 +137,23 @@ def respond(
81
 
82
  def main():
83
  """
84
- Sets up and launches the Gradio ChatInterface for the AI Teacher chatbot.
85
  """
86
  # Define default system message for an AI teacher
87
  default_system_message = (
88
  "You are an AI Teacher, a knowledgeable and patient educator dedicated to helping students and learners. "
89
  "Your goal is to explain concepts clearly, provide step-by-step guidance, and encourage critical thinking. "
90
  "Adapt your explanations to the learner's level, ask follow-up questions to deepen understanding, and provide examples where helpful. "
91
- "Be supportive, professional, and engaging in all interactions."
 
92
  )
93
 
94
- # Create Gradio ChatInterface with settings compatible with older Gradio versions
95
  demo = gr.ChatInterface(
96
  fn=respond,
97
  additional_inputs=[
 
 
98
  gr.Textbox(
99
  value=default_system_message,
100
  label="AI Teacher Prompt",
@@ -129,7 +188,8 @@ def main():
129
  title="AI Teacher: Your Study Companion",
130
  description=(
131
  "Welcome to AI Teacher, your personal guide for learning and studying! "
132
- "Ask questions about any subject, and I'll provide clear explanations, examples, and tips to help you succeed. "
 
133
  "Adjust the settings to customize how I respond to your questions."
134
  ),
135
  theme="soft",
 
2
  from huggingface_hub import InferenceClient
3
  from typing import List, Tuple
4
  import logging
5
+ import PyPDF2
6
+ import pytesseract
7
+ from PIL import Image
8
+ import io
9
 
10
  # Configure logging for better debugging and monitoring
11
  logging.basicConfig(
 
22
  logger.error(f"Failed to initialize InferenceClient: {str(e)}")
23
  raise
24
 
25
+ def extract_text_from_pdf(pdf_file) -> str:
26
+ """
27
+ Extracts text from an uploaded PDF file.
28
+ Args:
29
+ pdf_file: Path to the uploaded PDF file or file-like object.
30
+ Returns:
31
+ str: Extracted text from the PDF.
32
+ """
33
+ try:
34
+ text = ""
35
+ with open(pdf_file, "rb") as file:
36
+ reader = PyPDF2.PdfReader(file)
37
+ for page in reader.pages:
38
+ text += page.extract_text() or ""
39
+ logger.info("Successfully extracted text from PDF")
40
+ return text.strip()
41
+ except Exception as e:
42
+ logger.error(f"Error extracting text from PDF: {str(e)}")
43
+ return ""
44
+
45
+ def extract_text_from_image(image_file) -> str:
46
+ """
47
+ Extracts text from an uploaded image file using OCR.
48
+ Args:
49
+ image_file: Path to the uploaded image file or file-like object.
50
+ Returns:
51
+ str: Extracted text from the image.
52
+ """
53
+ try:
54
+ image = Image.open(image_file)
55
+ text = pytesseract.image_to_string(image)
56
+ logger.info("Successfully extracted text from image")
57
+ return text.strip()
58
+ except Exception as e:
59
+ logger.error(f"Error extracting text from image: {str(e)}")
60
+ return ""
61
+
62
  def respond(
63
  message: str,
64
+ pdf_file: str,
65
+ image_file: str,
66
  history: List[Tuple[str, str]],
67
  system_message: str,
68
  max_tokens: int,
 
70
  top_p: float,
71
  ) -> str:
72
  """
73
+ Generates an educational response to a student's query, including text from uploaded PDFs or images.
 
74
  Args:
75
  message (str): The student's input question or query.
76
+ pdf_file (str): Path to the uploaded PDF file.
77
+ image_file (str): Path to the uploaded image file.
78
  history (List[Tuple[str, str]]): Chat history with student and AI teacher messages.
79
  system_message (str): The system prompt defining the AI teacher's behavior.
80
  max_tokens (int): Maximum number of tokens to generate.
81
  temperature (float): Controls randomness in response generation.
82
  top_p (float): Controls diversity via nucleus sampling.
 
83
  Yields:
84
  str: The AI teacher's response, streamed token by token.
 
85
  Raises:
86
  ValueError: If input parameters are invalid.
87
  RuntimeError: If the API call fails.
88
  """
89
  # Validate input parameters
90
+ if not message.strip() and not pdf_file and not image_file:
91
+ raise ValueError("At least one of message, PDF, or image must be provided")
92
  if max_tokens < 1 or max_tokens > 2048:
93
  raise ValueError("max_tokens must be between 1 and 2048")
94
  if temperature < 0.1 or temperature > 2.0:
 
96
  if top_p < 0.1 or top_p > 1.0:
97
  raise ValueError("top_p must be between 0.1 and 1.0")
98
 
99
+ # Combine text from message, PDF, and image
100
+ combined_message = message.strip()
101
+ if pdf_file:
102
+ pdf_text = extract_text_from_pdf(pdf_file)
103
+ if pdf_text:
104
+ combined_message += "\n\n[From PDF]:\n" + pdf_text
105
+ if image_file:
106
+ image_text = extract_text_from_image(image_file)
107
+ if image_text:
108
+ combined_message += "\n\n[From Image]:\n" + image_text
109
+
110
+ if not combined_message.strip():
111
+ raise ValueError("No valid text extracted from inputs")
112
+
113
  # Construct the message history
114
  messages = [{"role": "system", "content": system_message}]
115
  for user_msg, assistant_msg in history:
 
117
  messages.append({"role": "user", "content": user_msg})
118
  if assistant_msg:
119
  messages.append({"role": "assistant", "content": assistant_msg})
120
+ messages.append({"role": "user", "content": combined_message})
121
 
122
  response = ""
123
  try:
 
137
 
138
  def main():
139
  """
140
+ Sets up and launches the Gradio ChatInterface for the AI Teacher chatbot with PDF and image upload support.
141
  """
142
  # Define default system message for an AI teacher
143
  default_system_message = (
144
  "You are an AI Teacher, a knowledgeable and patient educator dedicated to helping students and learners. "
145
  "Your goal is to explain concepts clearly, provide step-by-step guidance, and encourage critical thinking. "
146
  "Adapt your explanations to the learner's level, ask follow-up questions to deepen understanding, and provide examples where helpful. "
147
+ "Be supportive, professional, and engaging in all interactions. "
148
+ "If provided with text from uploaded PDFs or images, treat it as part of the student's question and respond accordingly."
149
  )
150
 
151
+ # Create Gradio ChatInterface with file upload components
152
  demo = gr.ChatInterface(
153
  fn=respond,
154
  additional_inputs=[
155
+ gr.File(label="Upload PDF", file_types=[".pdf"]),
156
+ gr.File(label="Upload Image", file_types=[".png", ".jpg", ".jpeg"]),
157
  gr.Textbox(
158
  value=default_system_message,
159
  label="AI Teacher Prompt",
 
188
  title="AI Teacher: Your Study Companion",
189
  description=(
190
  "Welcome to AI Teacher, your personal guide for learning and studying! "
191
+ "Ask questions by typing, or upload a PDF or image containing your question. "
192
+ "I'll provide clear explanations, examples, and tips to help you succeed. "
193
  "Adjust the settings to customize how I respond to your questions."
194
  ),
195
  theme="soft",