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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
+
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
| 5 |
+
|
| 6 |
+
example_input = '''
|
| 7 |
+
老师:这节课,我们就来一起认识一下Python最基础的3种数据类型。
|
| 8 |
+
|
| 9 |
+
没有很多,就3种,而且你基本都和他们打过照面了:
|
| 10 |
+
|
| 11 |
+
- 整数(int)
|
| 12 |
+
- 浮点数(float)
|
| 13 |
+
- 字符串(str)
|
| 14 |
+
|
| 15 |
+
<img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20230916111933.png"/>
|
| 16 |
+
|
| 17 |
+
### 整数int
|
| 18 |
+
|
| 19 |
+
<img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20230916221934.png"/>
|
| 20 |
+
|
| 21 |
+
整数(integer) 其实非常简单,就等同于我们小学数学里学到的“整数”概念。
|
| 22 |
+
|
| 23 |
+
我们上次让你print(42),这个 42 就是整数类型的。
|
| 24 |
+
|
| 25 |
+
你可以对整数进行各种数学运算,就像我们试过的print(1+2) 一样。
|
| 26 |
+
|
| 27 |
+
Python中的运算符号如下
|
| 28 |
+
<img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20230915181353.png"/>
|
| 29 |
+
|
| 30 |
+
我们可以搞个稍微复杂点的式子让python算一下:
|
| 31 |
+
|
| 32 |
+
```python
|
| 33 |
+
print((4+2)*3-8)
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
你可以尝试运行一下,应该是秒出结果,结果是10
|
| 37 |
+
|
| 38 |
+
学生:没错。果然是数学学霸~
|
| 39 |
+
|
| 40 |
+
老师:哈哈哈,没错。我们继续。
|
| 41 |
+
'''
|
| 42 |
+
|
| 43 |
+
example_output = '''·
|
| 44 |
+
```cleword
|
| 45 |
+
- 发言:
|
| 46 |
+
谁: 思远
|
| 47 |
+
说:
|
| 48 |
+
- 这节课,我们就来一起认识一下Python最基础的3种数据类型。
|
| 49 |
+
- 没有很多,就3种,而且你基本都和他们打过照面了:
|
| 50 |
+
|
| 51 |
+
- 图片:
|
| 52 |
+
地址: https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20230916111933.png
|
| 53 |
+
|
| 54 |
+
- 大纲:
|
| 55 |
+
标题: 整数int
|
| 56 |
+
等级: 2
|
| 57 |
+
|
| 58 |
+
- 图片:
|
| 59 |
+
地址: https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20230916221934.png
|
| 60 |
+
|
| 61 |
+
- 发言:
|
| 62 |
+
谁: 思远
|
| 63 |
+
说:
|
| 64 |
+
- 整数(integer) 其实非常简单,就等同于我们小学数学里学到的“整数”概念。
|
| 65 |
+
- 我们上次让你print(42),这个 42 就是整数类型的。
|
| 66 |
+
- 你可以对整数进行各种数学运算,就像我们试过的print(1+2) 一样。
|
| 67 |
+
- Python中的运算符号如下
|
| 68 |
+
- 图片:
|
| 69 |
+
地址: https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20230915181353.png
|
| 70 |
+
|
| 71 |
+
- 发言:
|
| 72 |
+
谁: 思远
|
| 73 |
+
说:
|
| 74 |
+
- 我们可以搞个稍微复杂点的式子让python算一下:
|
| 75 |
+
- |
|
| 76 |
+
```python
|
| 77 |
+
print((4+2)*3-8)
|
| 78 |
+
```
|
| 79 |
+
- 你可以尝试运行一下,应该是秒出结果,结果是10
|
| 80 |
+
|
| 81 |
+
- 发言:
|
| 82 |
+
谁: 学生
|
| 83 |
+
说:
|
| 84 |
+
- 没错。果然是数学学霸~
|
| 85 |
+
|
| 86 |
+
- 发言:
|
| 87 |
+
谁: 思远
|
| 88 |
+
说:
|
| 89 |
+
- 哈哈哈,没错。我们继续。
|
| 90 |
+
```
|
| 91 |
+
'''
|
| 92 |
+
|
| 93 |
+
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
|
| 94 |
+
response = openai.ChatCompletion.create(
|
| 95 |
+
model=model,
|
| 96 |
+
messages=messages,
|
| 97 |
+
temperature=temperature, # this is the degree of randomness of the model's output
|
| 98 |
+
)
|
| 99 |
+
# print(str(response.choices[0].message))
|
| 100 |
+
return response.choices[0].message["content"]
|
| 101 |
+
|
| 102 |
+
def generate_cleword(content):
|
| 103 |
+
|
| 104 |
+
system_message = "你是一名优秀的课程文稿录入员,擅长把markdown格式的课程文稿,转化为一种叫做cleword的领域特定语言。"
|
| 105 |
+
user_message = f"""
|
| 106 |
+
你是一名优秀的课程文稿录入员。我将给你markdown格式的课程文稿,请你帮助我把这种文稿转化为一种叫做cleword的领域特定语言。下面是一些转化的基本规则:
|
| 107 |
+
cleword是一种基于yaml的,缩进敏感的领域特定语言。它被专门用来创作课程。
|
| 108 |
+
以下是某门课内容的cleword语法示例,这门课的内容演示了一个名叫思远的老师,教给名叫大乾的学生关于Python的变量的知识的场景。请注意其中的语法规则和缩进:
|
| 109 |
+
|
| 110 |
+
```cleword
|
| 111 |
+
|
| 112 |
+
- 发言:
|
| 113 |
+
谁: 思远
|
| 114 |
+
说:
|
| 115 |
+
- ok,那现在请你在自己的电脑中打开python,我们开始接下来的学习✊✊
|
| 116 |
+
- 前面,我们学习了Python的print()函数和3种基本数据类型。这能够让Python把一些东西“说”给我们听。
|
| 117 |
+
|
| 118 |
+
- 图片:
|
| 119 |
+
地址: https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231026105853.png
|
| 120 |
+
|
| 121 |
+
- 发言:
|
| 122 |
+
谁: 思远
|
| 123 |
+
说:
|
| 124 |
+
- 但是目前,我们所有的程序都只是运行一步,把一个值打印出来。
|
| 125 |
+
- |
|
| 126 |
+
这对于更复杂的程序而言,肯定是不够用的。
|
| 127 |
+
- 数据如何在程序中被传递?
|
| 128 |
+
- 如何实现多个步骤的程序?
|
| 129 |
+
- 这些都是问题。
|
| 130 |
+
- 发言:
|
| 131 |
+
谁: 大乾
|
| 132 |
+
说:
|
| 133 |
+
- 复杂的程序?
|
| 134 |
+
- 发言:
|
| 135 |
+
谁: 思远
|
| 136 |
+
说:
|
| 137 |
+
- 举个具体点例子来说,
|
| 138 |
+
- 比如我们想用Python写一个进制转换的小程序,它能够自动把你输入进去的十进制进制的数字,转化为二进制数字。
|
| 139 |
+
- 这个程序的构架会大概长下面这样:
|
| 140 |
+
- 图片:
|
| 141 |
+
地址: https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231013113342.png
|
| 142 |
+
|
| 143 |
+
- 发言:
|
| 144 |
+
谁: 思远
|
| 145 |
+
说:
|
| 146 |
+
- 这里,我们需要 变量 来帮助这个程序做得更好。
|
| 147 |
+
|
| 148 |
+
- 大纲:
|
| 149 |
+
标题: 1 什么是变量
|
| 150 |
+
等级: 1
|
| 151 |
+
|
| 152 |
+
- 发言:
|
| 153 |
+
谁: 思远
|
| 154 |
+
说:
|
| 155 |
+
- |
|
| 156 |
+
虽然变量是个非常重要的东西,但其实它一点都不难,很容易理解。
|
| 157 |
+
> 只要你用心去做!
|
| 158 |
+
- 多说无益,先来尝试运行一下下面这个代码:
|
| 159 |
+
- |
|
| 160 |
+
```python
|
| 161 |
+
weather = '今天的天气是多云转晴'
|
| 162 |
+
print(weather)
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
- 旁白: 请自己在本地的编辑器里手打出来,不要复制粘贴哦
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
接下来,我将给你一个markdown版本的原始课程文稿。请你把它转化为cleword格式输出。
|
| 169 |
+
原始文稿:「{content}」
|
| 170 |
+
|
| 171 |
+
请你注意这里的markdown语法格式,并将原始文稿逐句地转化为符合cleword语法的格式。
|
| 172 |
+
|
| 173 |
+
"""
|
| 174 |
+
messages = [
|
| 175 |
+
{'role':'system',
|
| 176 |
+
'content': system_message},
|
| 177 |
+
{'role':'user',
|
| 178 |
+
'content': user_message},
|
| 179 |
+
]
|
| 180 |
+
response = get_completion_from_messages(messages)
|
| 181 |
+
return response
|
| 182 |
+
|
| 183 |
+
def app():
|
| 184 |
+
|
| 185 |
+
st.title('''Markdown-to-Cleword v0.0.1 🎉''')
|
| 186 |
+
|
| 187 |
+
col1, col2 = st.columns(2)
|
| 188 |
+
|
| 189 |
+
with col1:
|
| 190 |
+
script = st.text_area('请输入你想要转为cleword的markdown文稿:')
|
| 191 |
+
btn = st.button('提交')
|
| 192 |
+
|
| 193 |
+
with st.expander('输入示例'):
|
| 194 |
+
st.text_area('', value = example_input, height=500)
|
| 195 |
+
|
| 196 |
+
with st.expander('输出示例'):
|
| 197 |
+
st.markdown(example_output)
|
| 198 |
+
|
| 199 |
+
with col2:
|
| 200 |
+
st.markdown('转化结果如下👇')
|
| 201 |
+
if btn:
|
| 202 |
+
cleword = generate_cleword(script)
|
| 203 |
+
st.markdown(cleword)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
if __name__ == '__main__':
|
| 207 |
+
app()
|