SonyaX20 commited on
Commit
bf191d3
·
1 Parent(s): 4d0f186
Files changed (3) hide show
  1. .gitignore +22 -0
  2. README.md +29 -2
  3. app.py +133 -68
.gitignore ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Virtual Environment
2
+ venv/
3
+ env/
4
+ .env
5
+
6
+ # Python
7
+ __pycache__/
8
+ *.py[cod]
9
+ *$py.class
10
+
11
+ # IDE
12
+ .vscode/
13
+ .idea/
14
+
15
+ # Temporary files
16
+ temp_image.png
17
+ *.log
18
+
19
+ # Distribution / packaging
20
+ dist/
21
+ build/
22
+ *.egg-info/
README.md CHANGED
@@ -8,7 +8,34 @@ sdk_version: 5.0.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
- short_description: for final exams
12
  ---
13
 
14
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
+ short_description: 课程幻灯片智能理解助手
12
  ---
13
 
14
+ # 课程幻灯片理解助手
15
+
16
+ 这是一个基于 Gradio + EasyOCR + GPT-4 的课程幻灯片理解工具。
17
+
18
+ ## 功能特点
19
+
20
+ - 支持上传幻灯片图片
21
+ - 自动识别幻灯片中的文字内容(支持中英文)
22
+ - 智能解析幻灯片内容并提供讲解
23
+ - 支持与AI助手进行对话,深入理解课程内容
24
+
25
+ ## 技术栈
26
+
27
+ - Gradio 5.0.1:构建Web界面
28
+ - EasyOCR:图片文字识别
29
+ - OpenAI GPT-4:内容理解与对话
30
+ - Python 3.8+
31
+
32
+ ## 使用说明
33
+
34
+ 1. 上传幻灯片图片
35
+ 2. 点击"分析幻灯片"按钮
36
+ 3. 查看识别的文字内容和AI分析结果
37
+ 4. 使用对话框与AI助手交互,提出问题
38
+
39
+ ## License
40
+
41
+ MIT License
app.py CHANGED
@@ -15,107 +15,172 @@ client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
15
  # 初始化 EasyOCR
16
  reader = easyocr.Reader(['ch_sim', 'en'])
17
 
18
- def extract_text_from_image(image):
19
- """从图片中提取文字"""
20
- if isinstance(image, str):
21
- image_path = image
22
- else:
23
- # 将 numpy array 转换为 PIL Image
24
- if isinstance(image, np.ndarray):
25
- image = Image.fromarray(image)
26
- # 保存临时文件
27
- image_path = "temp_image.png"
28
- image.save(image_path)
29
 
30
- # 使用 EasyOCR 识别文字
31
- result = reader.readtext(image_path)
 
 
32
 
33
- # 如果是临时文件,删除它
34
- if image_path == "temp_image.png":
35
- os.remove(image_path)
36
 
37
- # 提取文字内容
38
- text = ' '.join([item[1] for item in result])
39
- return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  def analyze_slide(text):
42
  """使用 GPT-4 分析幻灯片内容"""
43
- prompt = f"""请分析以下幻灯片内容,并提供详细的讲解:
 
44
 
45
- {text}
46
 
47
- 请从以下几个方面进行分析:
48
- 1. 主要内容概述
49
- 2. 重点概念解释
50
- 3. 与其他知识的联系
51
- 4. 实际应用场景
52
 
53
- 请用中文回答。"""
54
 
55
- response = client.chat.completions.create(
56
- model="gpt-4",
57
- messages=[{"role": "user", "content": prompt}]
58
- )
59
-
60
- return response.choices[0].message.content
 
 
 
 
61
 
62
- def chat_with_assistant(message, history):
63
  """与 AI 助手对话"""
64
- messages = [
65
- {"role": "system", "content": "你是一位专业的课程助教,负责帮助学生理解课程内容。请用中文回答问题。"}
66
- ]
67
-
68
- # 添加历史对话
69
- for human, assistant in history:
70
- messages.append({"role": "user", "content": human})
71
- messages.append({"role": "assistant", "content": assistant})
72
 
73
- # 添加当前问题
74
- messages.append({"role": "user", "content": message})
75
-
76
- response = client.chat.completions.create(
77
- model="gpt-4",
78
- messages=messages
79
- )
80
-
81
- return response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  # 创建 Gradio 界面
84
  with gr.Blocks(title="课程幻灯片理解助手") as demo:
85
- gr.Markdown("# 课程幻灯片理解助手")
86
- gr.Markdown("上传幻灯片图片,AI 将帮助你理解内容并回答问题。")
 
 
 
87
 
88
  with gr.Row():
89
- with gr.Column():
90
- image_input = gr.Image(label="上传幻灯片图片")
91
- analyze_button = gr.Button("分析幻灯片")
 
 
 
92
 
93
- with gr.Column():
94
- text_output = gr.Textbox(label="识别的文字内容", lines=5)
95
- analysis_output = gr.Textbox(label="AI 分析结果", lines=10)
 
 
 
 
 
 
 
 
96
 
97
  gr.Markdown("---")
98
- gr.Markdown("### 与 AI 助手对话")
99
- chatbot = gr.Chatbot()
100
- msg = gr.Textbox(label="输入你的问题")
101
- clear = gr.Button("清除对话历史")
 
 
 
 
 
 
 
 
 
102
 
103
  # 设置事件处理
104
- analyze_button.click(
105
- fn=lambda img: (extract_text_from_image(img), analyze_slide(extract_text_from_image(img))),
106
  inputs=[image_input],
107
  outputs=[text_output, analysis_output]
 
 
 
 
108
  )
109
 
110
  msg.submit(
111
  fn=chat_with_assistant,
112
- inputs=[msg, chatbot],
113
  outputs=[chatbot],
114
  clear_input=True
115
  )
116
 
117
- clear.click(lambda: None, None, chatbot, queue=False)
118
 
119
  # 启动应用
120
  if __name__ == "__main__":
121
- demo.launch()
 
15
  # 初始化 EasyOCR
16
  reader = easyocr.Reader(['ch_sim', 'en'])
17
 
18
+ def process_image(image):
19
+ """处理上传的图片并返回识别结果和分析"""
20
+ if image is None:
21
+ return "请上传图片", "等待图片上传..."
 
 
 
 
 
 
 
22
 
23
+ # 提取文字
24
+ text = extract_text_from_image(image)
25
+ if not text.strip():
26
+ return "未能识别到文字内容,请尝试上传清晰的图片", "无法分析空白内容"
27
 
28
+ # 分析内容
29
+ analysis = analyze_slide(text)
 
30
 
31
+ return text, analysis
32
+
33
+ def extract_text_from_image(image):
34
+ """从图片中提取文字"""
35
+ try:
36
+ if isinstance(image, str):
37
+ image_path = image
38
+ else:
39
+ if isinstance(image, np.ndarray):
40
+ image = Image.fromarray(image)
41
+ image_path = "temp_image.png"
42
+ image.save(image_path)
43
+
44
+ # 使用 EasyOCR 识别文字
45
+ result = reader.readtext(image_path)
46
+
47
+ # 删除临时文件
48
+ if image_path == "temp_image.png" and os.path.exists(image_path):
49
+ os.remove(image_path)
50
+
51
+ # 按照位置排序并组织文字
52
+ sorted_text = []
53
+ for (bbox, text, prob) in result:
54
+ if prob > 0.5: # 只保留置信度大于 0.5 的结果
55
+ sorted_text.append(text)
56
+
57
+ return ' '.join(sorted_text)
58
+ except Exception as e:
59
+ return f"图片处理出错: {str(e)}"
60
 
61
  def analyze_slide(text):
62
  """使用 GPT-4 分析幻灯片内容"""
63
+ try:
64
+ prompt = f"""请分析以下幻灯片内容,并提供清晰的讲解:
65
 
66
+ 内容:{text}
67
 
68
+ 请按照以下结构组织回答:
69
+ 1. 主要内容:用2-3句话概括核心内容
70
+ 2. 重点解释:详细解释重要概念和关键点
71
+ 3. 知识延伸:与其他知识的联系
72
+ 4. 应用场景:在实际中的应用示例
73
 
74
+ 请用中文回答,语言要通俗易懂。"""
75
 
76
+ response = client.chat.completions.create(
77
+ model="gpt-4",
78
+ messages=[{"role": "user", "content": prompt}],
79
+ temperature=0.7,
80
+ max_tokens=1000
81
+ )
82
+
83
+ return response.choices[0].message.content
84
+ except Exception as e:
85
+ return f"内容分析出错: {str(e)}"
86
 
87
+ def chat_with_assistant(message, history, slide_text):
88
  """与 AI 助手对话"""
89
+ if not message:
90
+ return history
 
 
 
 
 
 
91
 
92
+ try:
93
+ context = f"""当前幻灯片内容:{slide_text}
94
+
95
+ 请基于以上幻灯片内容,回答用户的问题。如果问题与幻灯片内容无关,也可以回答其他问题。"""
96
+
97
+ messages = [
98
+ {"role": "system", "content": "你是一位专业的课程助教,负责帮助学生理解课程内容。请用清晰易懂的中文回答问题。"},
99
+ {"role": "user", "content": context}
100
+ ]
101
+
102
+ # 添加历史对话
103
+ for human, assistant in history:
104
+ messages.append({"role": "user", "content": human})
105
+ messages.append({"role": "assistant", "content": assistant})
106
+
107
+ messages.append({"role": "user", "content": message})
108
+
109
+ response = client.chat.completions.create(
110
+ model="gpt-4",
111
+ messages=messages,
112
+ temperature=0.7
113
+ )
114
+
115
+ history.append((message, response.choices[0].message.content))
116
+ return history
117
+ except Exception as e:
118
+ history.append((message, f"回答出错: {str(e)}"))
119
+ return history
120
 
121
  # 创建 Gradio 界面
122
  with gr.Blocks(title="课程幻灯片理解助手") as demo:
123
+ gr.Markdown("# 📚 课程幻灯片理解助手")
124
+ gr.Markdown("上传幻灯片图片,AI 将自动识别内容并提供详细讲解")
125
+
126
+ # 存储当前识别的文字,用于对话上下文
127
+ current_text = gr.State("")
128
 
129
  with gr.Row():
130
+ with gr.Column(scale=1):
131
+ image_input = gr.Image(
132
+ label="上传幻灯片图片",
133
+ type="pil",
134
+ tool="select"
135
+ )
136
 
137
+ with gr.Column(scale=2):
138
+ text_output = gr.Textbox(
139
+ label="识别的文字内容",
140
+ lines=3,
141
+ placeholder="上传图片后将显示识别的文字内容..."
142
+ )
143
+ analysis_output = gr.Textbox(
144
+ label="AI 讲解分析",
145
+ lines=10,
146
+ placeholder="等待分析结果..."
147
+ )
148
 
149
  gr.Markdown("---")
150
+ gr.Markdown("### 💬 与 AI 助手对话")
151
+ chatbot = gr.Chatbot(
152
+ label="对话历史",
153
+ height=400,
154
+ placeholder="在这里可以看到对话历史..."
155
+ )
156
+ with gr.Row():
157
+ msg = gr.Textbox(
158
+ label="输入你的问题",
159
+ placeholder="请输入你的问题...",
160
+ scale=4
161
+ )
162
+ clear = gr.Button("🗑️ 清除对话", scale=1)
163
 
164
  # 设置事件处理
165
+ image_input.change(
166
+ fn=process_image,
167
  inputs=[image_input],
168
  outputs=[text_output, analysis_output]
169
+ ).then(
170
+ fn=lambda x: x,
171
+ inputs=[text_output],
172
+ outputs=[current_text]
173
  )
174
 
175
  msg.submit(
176
  fn=chat_with_assistant,
177
+ inputs=[msg, chatbot, current_text],
178
  outputs=[chatbot],
179
  clear_input=True
180
  )
181
 
182
+ clear.click(lambda: ([], ""), outputs=[chatbot, msg])
183
 
184
  # 启动应用
185
  if __name__ == "__main__":
186
+ demo.launch(share=True)