Upload 3 files
Browse files- README.md +6 -5
- app.py +100 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: streamlit
|
| 7 |
-
sdk_version: 1.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: TestStreamlit
|
| 3 |
+
emoji: 📊
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: streamlit
|
| 7 |
+
sdk_version: 1.15.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: openrail
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from streamlit_chat import message
|
| 3 |
+
import os
|
| 4 |
+
import openai
|
| 5 |
+
|
| 6 |
+
#页面设置
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="小译学长|AI对话一问一答模式",
|
| 9 |
+
page_icon=":robot:"
|
| 10 |
+
)
|
| 11 |
+
st.header("🔥AI对话一问一答模式")
|
| 12 |
+
|
| 13 |
+
#检查账号登陆
|
| 14 |
+
def get_text1():
|
| 15 |
+
if 'openai_key' not in st.session_state:
|
| 16 |
+
input_text1 = st.text_input("📫请输入你的账号: ", key="input")
|
| 17 |
+
if st.button("确认登陆!", key="input3"):
|
| 18 |
+
st.session_state['openai_key'] = input_text1
|
| 19 |
+
return input_text1
|
| 20 |
+
else:
|
| 21 |
+
return st.session_state['openai_key']
|
| 22 |
+
|
| 23 |
+
openai_key = get_text1()
|
| 24 |
+
if openai_key:
|
| 25 |
+
openai.api_key = openai_key
|
| 26 |
+
st.write("")
|
| 27 |
+
else:
|
| 28 |
+
st.write("⚒️账号获取方式:扫描下方二维码或搜索关注微信公众号【正经人王同学】回复【小译学长】获取你的账号,然后将公共账号输入到此处,再两次点击【确认登陆!】就好")
|
| 29 |
+
st.image('https://pic4.zhimg.com/v2-401dd67cf027f85f53e4be3bd28dab5f_b.jpg')
|
| 30 |
+
|
| 31 |
+
# openAI code
|
| 32 |
+
def openai_create(prompt):
|
| 33 |
+
try:
|
| 34 |
+
|
| 35 |
+
response = openai.Completion.create(
|
| 36 |
+
model="text-davinci-003",
|
| 37 |
+
prompt=prompt,
|
| 38 |
+
temperature=0.5,
|
| 39 |
+
max_tokens=1024,
|
| 40 |
+
top_p=1,
|
| 41 |
+
frequency_penalty=0,
|
| 42 |
+
presence_penalty=0.6,
|
| 43 |
+
stop=[" Human:", " AI:"]
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
return response.choices[0].text
|
| 47 |
+
|
| 48 |
+
# except openai.error.UnauthorizedError:
|
| 49 |
+
# return "API Key不正确,请输入正确的API Key。"
|
| 50 |
+
# except openai.error.RateLimitError:
|
| 51 |
+
# Error="当前小译学长访问量过大,请过一段时间再试!开发者正在努力扩张服务能力,敬请期待!"
|
| 52 |
+
# return Error
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return "你的账号填写有误,请刷新页面重新填写正确的账号!"
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def chatgpt_clone(input):
|
| 59 |
+
output = openai_create(input)
|
| 60 |
+
return output
|
| 61 |
+
|
| 62 |
+
# Streamlit App
|
| 63 |
+
# st.set_page_config(
|
| 64 |
+
# page_title="小译AI对话单轮版",
|
| 65 |
+
# page_icon=":robot:"
|
| 66 |
+
# )
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# st.header("🏅和小译学长聊聊天")
|
| 72 |
+
|
| 73 |
+
if 'generated' not in st.session_state:
|
| 74 |
+
st.session_state['generated'] = []
|
| 75 |
+
|
| 76 |
+
if 'past' not in st.session_state:
|
| 77 |
+
st.session_state['past'] = []
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def get_text():
|
| 81 |
+
input_text = st.text_input("📫请告诉小译学长你想说的吧: ", key="input1")
|
| 82 |
+
if st.button("发送", key="input2"):
|
| 83 |
+
return input_text
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
st.markdown("""[正经人王同学|公众号](https://mp.weixin.qq.com/s?__biz=Mzg3ODcwNzk3Nw==&mid=2247485615&idx=1&sn=c691d496386b5972e36fea8eaee33b97&chksm=cf0edcc9f87955df95fa23a716d78d496e7456183da7cfa6b348db6988865da9060896abcdcd&token=1164458978&lang=zh_CN#rd) [抖音](https://www.douyin.com/user/MS4wLjABAAAAIdY0VlMSK0Shyd4FxHBgkXAtH4Zq8wsuKzIuSICWpy0) [小红书 ](https://www.xiaohongshu.com/user/profile/5f12a46a000000000101ff27)""")
|
| 87 |
+
|
| 88 |
+
user_input = get_text()
|
| 89 |
+
|
| 90 |
+
if user_input:
|
| 91 |
+
|
| 92 |
+
output = chatgpt_clone(user_input)
|
| 93 |
+
st.session_state.past.append(user_input)
|
| 94 |
+
st.session_state.generated.append(output)
|
| 95 |
+
|
| 96 |
+
if st.session_state['generated']:
|
| 97 |
+
|
| 98 |
+
for i in range(len(st.session_state['generated'])-1, -1, -1):
|
| 99 |
+
message(st.session_state["generated"][i], key=str(i))
|
| 100 |
+
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
streamlit_chat
|