alliwene commited on
Commit
33bc31b
·
1 Parent(s): 94a55c6

Upload folder using huggingface_hub

Browse files
Files changed (12) hide show
  1. .gitignore +2 -1
  2. audio/test-1.mp3 +0 -0
  3. audio/use.mp3 +0 -0
  4. backend.py +24 -4
  5. gen_app.py +60 -0
  6. generate_image.ipynb +0 -0
  7. images/lolly.png +0 -0
  8. notebook.ipynb +78 -13
  9. requirements.txt +1 -0
  10. result.py +0 -0
  11. test.py +31 -27
  12. work-app.py +10 -14
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  .env
2
- __pycache__
 
 
1
  .env
2
+ __pycache__
3
+ test*.ipynb
audio/test-1.mp3 ADDED
Binary file (167 kB). View file
 
audio/use.mp3 ADDED
Binary file (20.8 kB). View file
 
backend.py CHANGED
@@ -1,20 +1,40 @@
1
  import os
 
2
  from typing import List
3
 
4
  from dotenv import load_dotenv
5
  import openai
6
  import gradio as gr
 
7
 
 
8
 
9
- if load_dotenv():
10
- openai.api_key = os.getenv("OPENAI_API_KEY")
11
- else:
12
- openai.api_key = os.environ["OPENAI_API_KEY"]
13
 
14
 
15
  message_history = []
16
  cost = 0
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  def add_text(
20
  user_input: str, history: List, system_role: str = "You are a great assistant"
 
1
  import os
2
+ import time
3
  from typing import List
4
 
5
  from dotenv import load_dotenv
6
  import openai
7
  import gradio as gr
8
+ import numpy as np
9
 
10
+ load_dotenv()
11
 
12
+ openai.api_key = os.getenv("OPENAI_API_KEY")
13
+ # else:
14
+ # openai.api_key = os.environ["OPENAI_API_KEY"]
 
15
 
16
 
17
  message_history = []
18
  cost = 0
19
 
20
+ def transcribe(audio, state=""):
21
+ time.sleep(2)
22
+
23
+ transcript = openai.Audio.transcribe(
24
+ model="whisper-1", file=open(audio, "rb"), response_format="verbose_json"
25
+ )
26
+
27
+ text = transcript["text"]
28
+ cost += np.ceil(transcript["duration"])
29
+
30
+ return text
31
+
32
+ # for char in text:
33
+ # state += char
34
+
35
+ # yield state
36
+
37
+
38
 
39
  def add_text(
40
  user_input: str, history: List, system_role: str = "You are a great assistant"
gen_app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from typing import Literal, List
4
+ import requests
5
+
6
+ import openai
7
+ from PIL import Image as img
8
+ from PIL.Image import Image
9
+
10
+ import gradio as gr
11
+
12
+ load_dotenv()
13
+
14
+ openai.api_key = os.getenv("OPENAI_API_KEY")
15
+
16
+
17
+ def get_images(
18
+ prompt: str,
19
+ num_images=1,
20
+ img_size: Literal["256x256", "512x512", "1024x1024"] = "256x256",
21
+ ) -> List[Image]:
22
+ response = openai.Image.create(
23
+ prompt=prompt,
24
+ n=num_images,
25
+ size=img_size,
26
+ )
27
+
28
+ urls = [res["url"] for res in response["data"]]
29
+
30
+ images = [img.open(requests.get(url, stream=True).raw) for url in urls]
31
+
32
+ return images
33
+
34
+
35
+ with gr.Blocks() as demo:
36
+ with gr.Column(variant="panel"):
37
+ with gr.Row(variant="compact"):
38
+ text = gr.Textbox(
39
+ label="Enter your prompt",
40
+ show_label=False,
41
+ max_lines=1,
42
+ placeholder="Enter your prompt",
43
+ container=False,
44
+ )
45
+ btn = gr.Button("Generate image")
46
+
47
+ gallery = gr.Gallery(
48
+ label="Generated images",
49
+ show_label=False,
50
+ elem_id="gallery",
51
+ columns=2,
52
+ rows=2,
53
+ object_fit="contain",
54
+ height="auto",
55
+ )
56
+
57
+ btn.click(fn=get_images, inputs=text, outputs=gallery)
58
+
59
+ if __name__ == "__main__":
60
+ demo.launch()
generate_image.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
images/lolly.png ADDED
notebook.ipynb CHANGED
@@ -11,36 +11,101 @@
11
  },
12
  {
13
  "cell_type": "code",
14
- "execution_count": 4,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  "metadata": {},
16
  "outputs": [
17
  {
18
  "data": {
19
  "text/plain": [
20
- "True"
21
  ]
22
  },
23
- "execution_count": 4,
24
  "metadata": {},
25
  "output_type": "execute_result"
26
  }
27
  ],
28
  "source": [
29
- "import os\n",
30
- "from dotenv import load_dotenv\n",
31
- "\n",
32
- "import openai\n",
33
- "\n",
34
- "if load_dotenv():\n",
35
- " openai.api_key = os.getenv('OPENAI_API_KEY')\n"
36
  ]
37
  },
38
  {
39
  "cell_type": "code",
40
- "execution_count": 5,
41
  "metadata": {},
42
- "outputs": [],
43
- "source": []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  },
45
  {
46
  "cell_type": "code",
 
11
  },
12
  {
13
  "cell_type": "code",
14
+ "execution_count": 1,
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "import os\n",
19
+ "from dotenv import load_dotenv\n",
20
+ "\n",
21
+ "import openai\n",
22
+ "\n",
23
+ "if load_dotenv():\n",
24
+ " openai.api_key = os.getenv('OPENAI_API_KEY')\n"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": 2,
30
+ "metadata": {},
31
+ "outputs": [],
32
+ "source": [
33
+ "with open(\"audio/use.mp3\", \"rb\") as file:\n",
34
+ " transcript = openai.Audio.transcribe(\n",
35
+ " model=\"whisper-1\", file=file, response_format=\"verbose_json\"\n",
36
+ " )\n"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "code",
41
+ "execution_count": 15,
42
+ "metadata": {},
43
+ "outputs": [],
44
+ "source": [
45
+ "# transcript"
46
+ ]
47
+ },
48
+ {
49
+ "cell_type": "code",
50
+ "execution_count": 3,
51
  "metadata": {},
52
  "outputs": [
53
  {
54
  "data": {
55
  "text/plain": [
56
+ "'Hello.'"
57
  ]
58
  },
59
+ "execution_count": 3,
60
  "metadata": {},
61
  "output_type": "execute_result"
62
  }
63
  ],
64
  "source": [
65
+ "transcript['text']"
 
 
 
 
 
 
66
  ]
67
  },
68
  {
69
  "cell_type": "code",
70
+ "execution_count": 13,
71
  "metadata": {},
72
+ "outputs": [
73
+ {
74
+ "data": {
75
+ "text/plain": [
76
+ "9.0"
77
+ ]
78
+ },
79
+ "execution_count": 13,
80
+ "metadata": {},
81
+ "output_type": "execute_result"
82
+ }
83
+ ],
84
+ "source": [
85
+ "import numpy as np\n",
86
+ "np.ceil(transcript['duration'])"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": 14,
92
+ "metadata": {},
93
+ "outputs": [
94
+ {
95
+ "data": {
96
+ "text/plain": [
97
+ "0.054"
98
+ ]
99
+ },
100
+ "execution_count": 14,
101
+ "metadata": {},
102
+ "output_type": "execute_result"
103
+ }
104
+ ],
105
+ "source": [
106
+ "cost = np.ceil(transcript['duration']) * 0.006\n",
107
+ "cost"
108
+ ]
109
  },
110
  {
111
  "cell_type": "code",
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  gradio==3.35.2
 
2
  openai==0.27.8
3
  python-dotenv==1.0.0
 
1
  gradio==3.35.2
2
+ numpy==1.25.0
3
  openai==0.27.8
4
  python-dotenv==1.0.0
result.py ADDED
File without changes
test.py CHANGED
@@ -1,32 +1,36 @@
1
  import gradio as gr
 
2
 
 
 
3
 
4
- def sentence_builder(quantity, animal, countries, place, activity_list, morning):
5
- return f"""The {quantity} {animal}s from {" and ".join(countries)} went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}"""
6
-
7
-
8
- demo = gr.Interface(
9
- sentence_builder,
10
- [
11
- gr.Slider(2, 20, value=4, label="Count", info="Choose between 2 and 20"),
12
- gr.Dropdown(
13
- ["cat", "dog", "bird"], label="Animal", info="Will add more animals later!"
14
- ),
15
- gr.CheckboxGroup(["USA", "Japan", "Pakistan"], label="Countries", info="Where are they from?"),
16
- gr.Radio(["park", "zoo", "road"], label="Location", info="Where did they go?"),
17
- gr.Dropdown(
18
- ["ran", "swam", "ate", "slept"], value=["swam", "slept"], multiselect=True, label="Activity", info="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor, nisl eget ultricies aliquam, nunc nisl aliquet nunc, eget aliquam nisl nunc vel nisl."
19
- ),
20
- gr.Checkbox(label="Morning", info="Did they do it in the morning?"),
21
- ],
22
- "text",
23
- examples=[
24
- [2, "cat", ["Japan", "Pakistan"], "park", ["ate", "swam"], True],
25
- [4, "dog", ["Japan"], "zoo", ["ate", "swam"], False],
26
- [10, "bird", ["USA", "Pakistan"], "road", ["ran"], False],
27
- [8, "cat", ["Pakistan"], "zoo", ["ate"], True],
28
- ]
 
 
29
  )
30
 
31
- if __name__ == "__main__":
32
- demo.launch()
 
1
  import gradio as gr
2
+ import openai
3
 
4
+ from dotenv import load_dotenv
5
+ import os
6
 
7
+ load_dotenv()
8
+
9
+ openai.api_key = os.getenv("OPENAI_API_KEY")
10
+
11
+ messages = [{"role": "system", "content": "You are a teacher"}]
12
+
13
+
14
+ def transcribe(audio):
15
+ global messages
16
+ file = open(audio, "rb")
17
+ transcription = openai.Audio.transcribe("whisper-1", file)
18
+ print(transcription)
19
+ messages.append({"role": "user", "content": transcription["text"]})
20
+ response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
21
+
22
+ AImessage = response["choices"][0]["message"]["content"]
23
+
24
+ messages.append({"role": "assistant", "content": AImessage})
25
+ chat = ""
26
+ for message in messages:
27
+ if message["role"] != "system":
28
+ chat += message["role"] + ":" + message["content"] + "\n\n"
29
+ return chat
30
+
31
+
32
+ ui = gr.Interface(
33
+ fn=transcribe, inputs=gr.Audio(type="filepath"), outputs="text"
34
  )
35
 
36
+ ui.launch()
 
work-app.py CHANGED
@@ -1,27 +1,21 @@
1
  import gradio as gr
2
- from backend import generate_response, add_text, calc_cost
3
-
4
-
5
- # @click.command()
6
- # @click.option(
7
- # "-s",
8
- # "--system_role",
9
- # default="You are good assistant",
10
- # type=str,
11
- # help="Which role do you want the chatbot to take in replying your messages",
12
- # )
13
- # def app():
14
  with gr.Blocks() as demo:
15
  chatbot = gr.Chatbot()
16
 
17
  with gr.Row():
 
 
 
18
  with gr.Column(scale=0.9):
19
  message = gr.Textbox(
20
- show_label=False,
21
  placeholder="Please enter a message and press Enter",
22
  )
23
 
24
- with gr.Column(scale=0.1):
25
  cost_view = gr.Number(label="Usage in $", value=0)
26
 
27
  clear = gr.ClearButton([chatbot, message, cost_view])
@@ -32,6 +26,8 @@ with gr.Blocks() as demo:
32
  info="Which openai chat model to use",
33
  )
34
 
 
 
35
  response = (
36
  message.submit(add_text, [message, chatbot], [message, chatbot], queue=False)
37
  .then(generate_response, [chatbot, models], chatbot)
 
1
  import gradio as gr
2
+ from backend import generate_response, add_text, calc_cost, transcribe
3
+
4
+
 
 
 
 
 
 
 
 
 
5
  with gr.Blocks() as demo:
6
  chatbot = gr.Chatbot()
7
 
8
  with gr.Row():
9
+ # with gr.Column(scale=0.05):
10
+ # audio = gr.Audio(type="filepath")
11
+
12
  with gr.Column(scale=0.9):
13
  message = gr.Textbox(
14
+ label="\n",
15
  placeholder="Please enter a message and press Enter",
16
  )
17
 
18
+ with gr.Column(scale=0.05):
19
  cost_view = gr.Number(label="Usage in $", value=0)
20
 
21
  clear = gr.ClearButton([chatbot, message, cost_view])
 
26
  info="Which openai chat model to use",
27
  )
28
 
29
+ # audio.upload(transcribe, inputs=[audio], outputs=[message])
30
+
31
  response = (
32
  message.submit(add_text, [message, chatbot], [message, chatbot], queue=False)
33
  .then(generate_response, [chatbot, models], chatbot)