Spaces:
Running
Running
雷娃
commited on
Commit
·
742582e
1
Parent(s):
9ece560
init app
Browse files- app.py +113 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 3 |
+
from threading import Thread
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import re
|
| 6 |
+
import torch
|
| 7 |
+
from openai import OpenAI
|
| 8 |
+
|
| 9 |
+
#client = OpenAI(
|
| 10 |
+
# api_key="sk-420ab66020704eabbe37501ec39b7a2b",
|
| 11 |
+
# base_url="https://bailingchat.alipay.com",
|
| 12 |
+
#)
|
| 13 |
+
|
| 14 |
+
client = OpenAI(
|
| 15 |
+
api_key="sk-evmlzmwzibqqipnpetyryfxxrbsxeucctkrbppdevuyjvont",
|
| 16 |
+
base_url="https://api.siliconflow.cn/v1",
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# define chat function
|
| 20 |
+
def chat(user_input, max_tokens=11264):
|
| 21 |
+
# chat history
|
| 22 |
+
messages_template = [
|
| 23 |
+
# {"role": "system", "content": "You are Ling, an assistant created by inclusionAI"},
|
| 24 |
+
{"role": "user", "content": user_input}
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
response = client.chat.completions.create(
|
| 28 |
+
model="inclusionAI/Ling-mini-2.0",
|
| 29 |
+
messages=messages_template,
|
| 30 |
+
max_tokens=max_tokens,
|
| 31 |
+
temperature=0.01,
|
| 32 |
+
top_p=1,
|
| 33 |
+
stream=True,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
def generate():
|
| 37 |
+
pass
|
| 38 |
+
|
| 39 |
+
resp_text = ""
|
| 40 |
+
thread = Thread(target=generate)
|
| 41 |
+
thread.start()
|
| 42 |
+
|
| 43 |
+
for chunk in response:
|
| 44 |
+
if chunk.choices[0].delta.content is not None:
|
| 45 |
+
resp_text += chunk.choices[0].delta.content
|
| 46 |
+
yield resp_text
|
| 47 |
+
print(resp_text)
|
| 48 |
+
|
| 49 |
+
thread.join()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Create a custom layout using Blocks
|
| 54 |
+
with gr.Blocks(css="""
|
| 55 |
+
#markdown-output {
|
| 56 |
+
height: 300px;
|
| 57 |
+
overflow-y: auto;
|
| 58 |
+
border: 1px solid #ddd;
|
| 59 |
+
padding: 10px;
|
| 60 |
+
}
|
| 61 |
+
""") as demo:
|
| 62 |
+
gr.Markdown(
|
| 63 |
+
"## Ling-mini-2.0 AI Assistant\n"
|
| 64 |
+
"Based on [inclusionAI/Ling-mini-2.0](https://huggingface.co/inclusionAI/Ling-mini-2.0)\n"
|
| 65 |
+
# "Access through [Ling API](https://bailingchat.alipay.com)"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
max_tokens_slider = gr.Slider(minimum=5000, maximum=10000, step=100, label="Generated length")
|
| 70 |
+
|
| 71 |
+
# output_box = gr.Textbox(lines=10, label="Response")
|
| 72 |
+
output_box = gr.Markdown(label="Response", elem_id="markdown-output")
|
| 73 |
+
input_box = gr.Textbox(lines=8, label="Input you question")
|
| 74 |
+
|
| 75 |
+
examples = gr.Examples(
|
| 76 |
+
examples=[
|
| 77 |
+
["Introducing the basic concepts of large language models"],
|
| 78 |
+
["How to solve long context dependencies in math problems?"]
|
| 79 |
+
],
|
| 80 |
+
inputs=input_box
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
interface = gr.Interface(
|
| 84 |
+
fn=chat,
|
| 85 |
+
inputs=[input_box, max_tokens_slider],
|
| 86 |
+
outputs=output_box,
|
| 87 |
+
live=False # disable auto-triggering on input change
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# launch Gradio Service
|
| 91 |
+
demo.queue()
|
| 92 |
+
demo.launch()
|
| 93 |
+
|
| 94 |
+
# Construct Gradio Interface
|
| 95 |
+
#interface = gr.Interface(
|
| 96 |
+
# fn=chat,
|
| 97 |
+
# inputs=[
|
| 98 |
+
# gr.Textbox(lines=8, label="输入你的问题"),
|
| 99 |
+
# gr.Slider(minimum=100, maximum=102400, step=50, label="生成长度")
|
| 100 |
+
# ],
|
| 101 |
+
# outputs=[
|
| 102 |
+
# gr.Textbox(lines=8, label="模型回复")
|
| 103 |
+
# ],
|
| 104 |
+
# title="Ling-lite-2.0 AI助手",
|
| 105 |
+
# description="基于 [inclusionAI/Ling-mini-2.0](https://huggingface.co/inclusionAI/Ling-mini-2.0) 的对话式文本生成演示。",
|
| 106 |
+
# examples=[
|
| 107 |
+
# ["介绍大型语言模型的基本概念"],
|
| 108 |
+
# ["如何解决数学问题中的长上下文依赖?"]
|
| 109 |
+
# ]
|
| 110 |
+
#)
|
| 111 |
+
|
| 112 |
+
# launch Gradion Service
|
| 113 |
+
#interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
accelerate
|
| 5 |
+
openai
|