SonyaX20 commited on
Commit
4d0f186
·
1 Parent(s): 0e5ebcb
Files changed (4) hide show
  1. .env +1 -0
  2. .gitattributes +4 -0
  3. app.py +107 -50
  4. requirements.txt +7 -1
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY=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
.gitattributes CHANGED
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.py text eol=lf
37
+ *.txt text eol=lf
38
+ *.md text eol=lf
39
+ .env text eol=lf
app.py CHANGED
@@ -1,64 +1,121 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
27
 
28
- response = ""
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
 
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ import easyocr
4
+ from dotenv import load_dotenv
5
+ from openai import OpenAI
6
+ from PIL import Image
7
+ import numpy as np
8
 
9
+ # 加载环境变量
10
+ load_dotenv()
 
 
11
 
12
+ # 初始化 OpenAI 客户端
13
+ client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
14
 
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()
requirements.txt CHANGED
@@ -1 +1,7 @@
1
- huggingface_hub==0.25.2
 
 
 
 
 
 
 
1
+ huggingface_hub==0.25.2
2
+ gradio>=4.0.0
3
+ easyocr>=1.7.1
4
+ python-dotenv>=1.0.0
5
+ openai>=1.0.0
6
+ Pillow>=10.0.0
7
+ numpy>=1.24.0