CoolFace
Apppublic

zai-org/CodeGeeX

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
327likes
app.py65 linesDownload Raw Back to root
1import os2 3import gradio as gr4from zhipuai import ZhipuAI5 6SYSTEM_PROMPT = "你是一位智能编程助手,你叫CodeGeeX。你会为用户回答关于编程、代码、计算机方面的任何问题,并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。"7 8client = ZhipuAI(api_key=os.getenv("CODEGEEX_API_KEY"))9 10def respond(message, history: list[tuple[str, str]]):11    messages = [{"role": "system", "content": SYSTEM_PROMPT}]12 13    for val in history:14        if val[0]:15            messages.append({"role": "user", "content": val[0]})16        if val[1]:17            messages.append({"role": "assistant", "content": val[1]})18 19    messages.append({"role": "user", "content": message})20 21    response = ""22 23    for message in client.chat.completions.create(24        messages=messages,  # type: ignore25        model="codegeex-4",26        stream=True,27        temperature=0.2,28        max_tokens=1024,29        top_p=0.95,30    ):  # type: ignore31        token = message.choices[0].delta.content32 33        response += token34        yield response35 36 37with gr.Blocks(fill_height=True) as demo:38    gr.Markdown(39        """40            <p align="center" style="margin: 32px 32px 0 0;">41                <img src="https://gist.githubusercontent.com/rojas-diego/0c1b444aff2c6b6420ff635bfd206869/raw/16566317fabce71d35ab3cf8c71adf3b5dc11d87/codegeex.svg" style="width: 30%">42            </p>43            """44    )45    gr.Markdown(46        """47        <p align="center">48            🏠 <a href="https://codegeex.cn" target="_blank">Homepage</a> | 📖 <a href="http://keg.cs.tsinghua.edu.cn/codegeex/" target="_blank">Blog</a> | 🛠 <a href="https://marketplace.visualstudio.com/items?itemName=aminer.codegeex" target="_blank">VS Code</a> or <a href="https://plugins.jetbrains.com/plugin/20587-codegeex" target="_blank">Jetbrains</a> Extensions | 💻 <a href="https://github.com/THUDM/CodeGeeX4" target="_blank">Github</a> | 🤖 <a href="https://huggingface.co/THUDM/codegeex-4-9b" target="_blank">HuggingFace</a>49        </p>50        """51    )52    gr.Markdown(53        """54        <p align="center">55            We introduce CodeGeeX4 9B, a large-scale multilingual code generation model with 9 billion parameters, pre-trained on a large code corpus of more than 300 programming languages. CodeGeeX4 9B is open source, please refer to our <a href="https://github.com/THUDM/CodeGeeX4" target="_blank">GitHub</a> for more details. We also offer free <a href="https://marketplace.visualstudio.com/items?itemName=aminer.codegeex" target="_blank">VS Code</a> and <a href="https://plugins.jetbrains.com/plugin/20587-codegeex" target="_blank">Jetbrains</a> extensions for full functionality.56        </p>57        """58    )59 60    gr.ChatInterface(respond, fill_height=True)61 62 63if __name__ == "__main__":64    demo.launch()65