Spaces:
Runtime error
Runtime error
SonyaX20
commited on
Commit
·
4d0f186
1
Parent(s):
0e5ebcb
first
Browse files- .env +1 -0
- .gitattributes +4 -0
- app.py +107 -50
- 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 |
-
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
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 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
27 |
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
yield response
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
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
|