emeses commited on
Commit
38853b8
·
1 Parent(s): 6563121

Update space

Browse files
Files changed (2) hide show
  1. app.py +59 -21
  2. requirements.txt +4 -6
app.py CHANGED
@@ -20,7 +20,7 @@ def respond(message, history, max_tokens=512, temperature=0.7, top_p=0.95):
20
  response = ""
21
  for chunk in client.chat_completion(
22
  messages,
23
- max_tokens=max_tokens, # Changed back to max_tokens
24
  temperature=temperature,
25
  top_p=top_p,
26
  stream=True,
@@ -33,7 +33,7 @@ def respond(message, history, max_tokens=512, temperature=0.7, top_p=0.95):
33
 
34
  except Exception as e:
35
  return f"Error: {str(e)}"
36
-
37
  def extract_schedule(url):
38
  try:
39
  # Fetch and parse webpage
@@ -48,35 +48,78 @@ def extract_schedule(url):
48
 
49
  schedule_data = []
50
  rows = table.find_all('tr')
51
-
52
- for row in rows[1:]: # Skip header row
53
  cells = row.find_all('td')
54
- if len(cells) >= 4: # Only process rows with enough columns
55
  date = cells[0].text.strip()
56
  topic = cells[1].text.strip()
57
 
58
- # Skip empty rows and non-lecture entries
59
  if date and topic and not topic.startswith('See Canvas'):
60
  schedule_data.append({
61
- 'Date': date[:10], # Only YYYY-MM-DD
62
- 'Topic': topic
 
 
 
 
 
 
 
63
  })
64
-
65
- # Create DataFrame
66
  df = pd.DataFrame(schedule_data)
67
 
68
- # Convert to HTML with styling
69
  html = f"""
70
  <style>
71
- table {{ border-collapse: collapse; width: 100%; }}
 
 
 
 
72
  th, td {{
73
  border: 1px solid black;
74
- padding: 8px;
75
  text-align: left;
 
 
 
 
 
 
76
  }}
77
- th {{ background-color: #f2f2f2; }}
78
  </style>
79
- {df.to_html(index=False)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  """
81
  return html
82
 
@@ -110,12 +153,7 @@ with gr.Blocks() as demo:
110
  # Right Column - Chatbot
111
  with gr.Column(scale=2):
112
  chatbot = gr.ChatInterface(
113
- respond,
114
- #additional_inputs=[
115
- # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
116
- # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
117
- # gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
118
- #]
119
  )
120
 
121
  if __name__ == "__main__":
 
20
  response = ""
21
  for chunk in client.chat_completion(
22
  messages,
23
+ max_tokens=max_tokens,
24
  temperature=temperature,
25
  top_p=top_p,
26
  stream=True,
 
33
 
34
  except Exception as e:
35
  return f"Error: {str(e)}"
36
+
37
  def extract_schedule(url):
38
  try:
39
  # Fetch and parse webpage
 
48
 
49
  schedule_data = []
50
  rows = table.find_all('tr')
51
+ for row in rows[1:]:
 
52
  cells = row.find_all('td')
53
+ if len(cells) >= 4:
54
  date = cells[0].text.strip()
55
  topic = cells[1].text.strip()
56
 
 
57
  if date and topic and not topic.startswith('See Canvas'):
58
  schedule_data.append({
59
+ 'Date': date[:10],
60
+ 'Topic': topic,
61
+ 'Action': f"""
62
+ <button
63
+ onclick="triggerChatbotPreparation('{topic.replace("'", "")}')"
64
+ class="prepare-btn">
65
+ Prepare
66
+ </button>
67
+ """
68
  })
69
+
 
70
  df = pd.DataFrame(schedule_data)
71
 
72
+ # Convert to HTML with styling and JavaScript
73
  html = f"""
74
  <style>
75
+ table {{
76
+ border-collapse: collapse;
77
+ width: 100%;
78
+ font-size: 12px;
79
+ }}
80
  th, td {{
81
  border: 1px solid black;
82
+ padding: 6px;
83
  text-align: left;
84
+ font-family: Arial, sans-serif;
85
+ }}
86
+ .prepare-btn {{
87
+ padding: 4px 8px;
88
+ font-size: 11px;
89
+ cursor: pointer;
90
  }}
 
91
  </style>
92
+ <script>
93
+ function triggerChatbotPreparation(topic) {{
94
+ // Find all Gradio textareas
95
+ const textareas = document.querySelectorAll('.gradio-container textarea');
96
+
97
+ // Find the first textarea (assuming it's the input)
98
+ const textbox = textareas[0];
99
+
100
+ if (textbox) {{
101
+ // Set the value
102
+ const preparationMessage = `prepare 5 minutes reading important parts related to ${topic}`;
103
+ textbox.value = preparationMessage;
104
+
105
+ // Trigger input and change events
106
+ const inputEvent = new Event('input', {{ bubbles: true }});
107
+ const changeEvent = new Event('change', {{ bubbles: true }});
108
+ textbox.dispatchEvent(inputEvent);
109
+ textbox.dispatchEvent(changeEvent);
110
+
111
+ // Find and click the send button
112
+ const sendButtons = document.querySelectorAll('.gradio-container button');
113
+ for (let button of sendButtons) {{
114
+ if (button.getAttribute('aria-label') === 'Send') {{
115
+ button.click();
116
+ break;
117
+ }}
118
+ }}
119
+ }}
120
+ }}
121
+ </script>
122
+ {df.to_html(index=False, escape=False)}
123
  """
124
  return html
125
 
 
153
  # Right Column - Chatbot
154
  with gr.Column(scale=2):
155
  chatbot = gr.ChatInterface(
156
+ respond
 
 
 
 
 
157
  )
158
 
159
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -1,7 +1,5 @@
1
- transformers
2
- huggingface_hub==0.25.2
3
  gradio==5.8.0
4
- torch
5
- bitsandbytes
6
- accelerate
7
- unsloth
 
 
 
1
  gradio==5.8.0
2
+ huggingface_hub==0.25.2
3
+ pandas
4
+ requests
5
+ beautifulsoup4