ericwangpq commited on
Commit
1912fdc
·
1 Parent(s): c78d3b2

4-19 1159

Browse files
Files changed (1) hide show
  1. debugging.ipynb +113 -0
debugging.ipynb ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 6,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stdout",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "Running on local URL: http://127.0.0.1:7863\n",
13
+ "Running on public URL: https://e43e3020a427152c81.gradio.live\n",
14
+ "\n",
15
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
16
+ ]
17
+ },
18
+ {
19
+ "data": {
20
+ "text/html": [
21
+ "<div><iframe src=\"https://e43e3020a427152c81.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
22
+ ],
23
+ "text/plain": [
24
+ "<IPython.core.display.HTML object>"
25
+ ]
26
+ },
27
+ "metadata": {},
28
+ "output_type": "display_data"
29
+ },
30
+ {
31
+ "data": {
32
+ "text/plain": []
33
+ },
34
+ "execution_count": 6,
35
+ "metadata": {},
36
+ "output_type": "execute_result"
37
+ }
38
+ ],
39
+ "source": [
40
+ "import gradio as gr\n",
41
+ "\n",
42
+ "from openai import OpenAI\n",
43
+ "import gradio as gr\n",
44
+ "import os\n",
45
+ "\n",
46
+ "#API_KEY = os.getenv(\"PPLX_API_KEY\")\n",
47
+ "API_KEY = \"os123\"\n",
48
+ "client = OpenAI(api_key=API_KEY, base_url=\"https://api.perplexity.ai\")\n",
49
+ "\n",
50
+ "def predict(message, history):\n",
51
+ " history_openai_format = []\n",
52
+ " for human, assistant in history:\n",
53
+ " history_openai_format.append({\"role\": \"user\", \"content\": human})\n",
54
+ " history_openai_format.append({\"role\": \"assistant\", \"content\": assistant})\n",
55
+ " history_openai_format.append({\"role\": \"user\", \"content\": message})\n",
56
+ " \n",
57
+ " # 使用 Perplexity AI 的 70B 模型进行响应\n",
58
+ " response = client.chat.completions.create(\n",
59
+ " model='pplx-70b-online',\n",
60
+ " messages=history_openai_format,\n",
61
+ " temperature=1.0,\n",
62
+ " stream=True\n",
63
+ " )\n",
64
+ "\n",
65
+ " partial_message = \"\"\n",
66
+ " for chunk in response:\n",
67
+ " if chunk.choices[0].delta.content is not None:\n",
68
+ " partial_message += chunk.choices[0].delta.content\n",
69
+ " yield partial_message\n",
70
+ "\n",
71
+ "def change_textbox(choice):\n",
72
+ " #根据不同输入对输出控件进行更新\n",
73
+ " if choice == \"short\":\n",
74
+ " return gr.update(lines=2, visible=True, value=\"Short story: \")\n",
75
+ " elif choice == \"long\":\n",
76
+ " return gr.update(lines=8, visible=True, value=\"Long story...\")\n",
77
+ " else:\n",
78
+ " return gr.update(visible=False)\n",
79
+ "\n",
80
+ "with gr.Blocks(fill_height=True) as demo:\n",
81
+ " radio = gr.Radio(\n",
82
+ " [\"short\", \"long\", \"none\"], label=\"Essay Length to Write?\"\n",
83
+ " )\n",
84
+ " text = gr.Textbox(lines=2, interactive=True)\n",
85
+ " radio.change(fn=change_textbox, inputs=radio, outputs=text)\n",
86
+ " chat_interface = gr.ChatInterface(predict)\n",
87
+ "\n",
88
+ "demo.launch(share=True)\n"
89
+ ]
90
+ }
91
+ ],
92
+ "metadata": {
93
+ "kernelspec": {
94
+ "display_name": "base",
95
+ "language": "python",
96
+ "name": "python3"
97
+ },
98
+ "language_info": {
99
+ "codemirror_mode": {
100
+ "name": "ipython",
101
+ "version": 3
102
+ },
103
+ "file_extension": ".py",
104
+ "mimetype": "text/x-python",
105
+ "name": "python",
106
+ "nbconvert_exporter": "python",
107
+ "pygments_lexer": "ipython3",
108
+ "version": "3.11.7"
109
+ }
110
+ },
111
+ "nbformat": 4,
112
+ "nbformat_minor": 2
113
+ }