Clean up logging and add timestamps.
Browse files
app.py
CHANGED
|
@@ -1,16 +1,30 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from pathlib import Path
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
| 5 |
import torch
|
| 6 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 12 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 13 |
|
|
|
|
| 14 |
def init_speech_to_text_model():
|
| 15 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 16 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
@@ -72,14 +86,10 @@ frame.contentWindow.document.close();
|
|
| 72 |
|
| 73 |
# TODO: Works but is inefficient because the iframe has to be reloaded each time
|
| 74 |
update_iframe_js = f"""(code) => {{
|
| 75 |
-
console.log(`UPDATING CODE`);
|
| 76 |
-
console.log(code)
|
| 77 |
const pattern = /# APP CODE START(.*?)# APP CODE END/gs;
|
| 78 |
const template = `{html_template}`;
|
| 79 |
const completedTemplate = template.replace(pattern, code);
|
| 80 |
|
| 81 |
-
console.log(completedTemplate);
|
| 82 |
-
|
| 83 |
const oldFrame = document.querySelector('.my-frame');
|
| 84 |
oldFrame.remove();
|
| 85 |
|
|
@@ -94,7 +104,6 @@ const frame = document.querySelector('.my-frame');
|
|
| 94 |
frame.contentWindow.document.open('text/html', 'replace');
|
| 95 |
frame.contentWindow.document.write(completedTemplate);
|
| 96 |
frame.contentWindow.document.close();
|
| 97 |
-
console.log(`UPDATE DONE`);
|
| 98 |
}}"""
|
| 99 |
|
| 100 |
copy_snippet_js = f"""async (code) => {{
|
|
@@ -107,15 +116,10 @@ const template = `<div id="KiteWindApp">\n<script type="module" crossorigin src=
|
|
| 107 |
const completedTemplate = template.replace(pattern, code);
|
| 108 |
|
| 109 |
const snippet = completedTemplate;
|
| 110 |
-
console.log(snippet);
|
| 111 |
-
|
| 112 |
await navigator.clipboard.writeText(snippet);
|
| 113 |
-
|
| 114 |
-
console.log(`COPY DONE`);
|
| 115 |
}}"""
|
| 116 |
|
| 117 |
download_code_js = f"""(code) => {{
|
| 118 |
-
console.log(`DOWNLOADING CODE`);
|
| 119 |
const pattern = /# APP CODE START(.*?)# APP CODE END/gs;
|
| 120 |
const template = `{html_template}`;
|
| 121 |
// Step 1: Generate the HTML content
|
|
@@ -137,8 +141,6 @@ downloadLink.click();
|
|
| 137 |
|
| 138 |
// Clean up by revoking the URL
|
| 139 |
URL.revokeObjectURL(url);
|
| 140 |
-
|
| 141 |
-
console.log(`DOWNLOAD DONE`);
|
| 142 |
}}"""
|
| 143 |
|
| 144 |
|
|
@@ -148,25 +150,28 @@ def query(payload):
|
|
| 148 |
|
| 149 |
|
| 150 |
def generate_text(code, prompt):
|
| 151 |
-
|
| 152 |
prompt = f"```python\n{code}```\nGiven the code above return only updated code for the following request:\n{prompt}\n<|assistant|>"
|
| 153 |
params = {"max_new_tokens": 512}
|
| 154 |
output = query({
|
| 155 |
"inputs": prompt,
|
| 156 |
"parameters": params,
|
| 157 |
})
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
| 159 |
assistant_reply = output[0]["generated_text"].split('<|assistant|>')[1]
|
| 160 |
match = re.search(code_pattern, assistant_reply, re.DOTALL)
|
| 161 |
new_code = match.group(1)
|
| 162 |
-
|
| 163 |
# TODO: error handling here
|
| 164 |
return assistant_reply, new_code, None
|
| 165 |
|
| 166 |
|
| 167 |
def transcribe(audio):
|
| 168 |
result = whisper_pipe(audio)
|
| 169 |
-
return result["text"]
|
| 170 |
|
| 171 |
|
| 172 |
def copy_notify(code):
|
|
@@ -175,7 +180,8 @@ def copy_notify(code):
|
|
| 175 |
|
| 176 |
with gr.Blocks() as demo:
|
| 177 |
gr.Markdown("<h1 align=\"center\">KiteWind πͺπ</h1>")
|
| 178 |
-
gr.Markdown(
|
|
|
|
| 179 |
with gr.Row():
|
| 180 |
with gr.Column():
|
| 181 |
gr.Markdown("## 1. Run your app in the browser!")
|
|
@@ -190,11 +196,15 @@ with gr.Blocks() as demo:
|
|
| 190 |
out_text = gr.TextArea(label="Chat Assistant Response")
|
| 191 |
clear = gr.ClearButton([in_prompt, in_audio, out_text])
|
| 192 |
with gr.Column():
|
| 193 |
-
code_area = gr.Code(label="App Code - You can also edit directly and then click Update App",
|
|
|
|
| 194 |
update_btn = gr.Button("Update App", variant="primary")
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
| 198 |
with gr.Row():
|
| 199 |
with gr.Column():
|
| 200 |
gr.Markdown("## 3. Export your app to share!")
|
|
@@ -206,7 +216,8 @@ with gr.Blocks() as demo:
|
|
| 206 |
with gr.Column():
|
| 207 |
gr.Markdown("## Current limitations")
|
| 208 |
with gr.Accordion("Click to view", open=False):
|
| 209 |
-
gr.Markdown(
|
|
|
|
| 210 |
|
| 211 |
demo.load(None, None, None, _js=load_js)
|
| 212 |
demo.css = "footer {visibility: hidden}"
|
|
|
|
| 1 |
+
import logging
|
| 2 |
import os
|
| 3 |
+
import re
|
| 4 |
from pathlib import Path
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
+
import requests
|
| 8 |
import torch
|
| 9 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 10 |
+
|
| 11 |
+
logging.basicConfig(
|
| 12 |
+
level=logging.INFO, # Set the logging level to INFO or any other desired level
|
| 13 |
+
format="%(asctime)s - %(message)s", # Define the log message format
|
| 14 |
+
datefmt="%Y-%m-%d %H:%M:%S", # Define the timestamp format
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
logger = logging.getLogger("my_logger")
|
| 18 |
|
| 19 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 20 |
|
| 21 |
+
if not HF_TOKEN:
|
| 22 |
+
raise Exception("HF_TOKEN environment variable is required to call remote API.")
|
| 23 |
+
|
| 24 |
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 25 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 26 |
|
| 27 |
+
|
| 28 |
def init_speech_to_text_model():
|
| 29 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 30 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
| 86 |
|
| 87 |
# TODO: Works but is inefficient because the iframe has to be reloaded each time
|
| 88 |
update_iframe_js = f"""(code) => {{
|
|
|
|
|
|
|
| 89 |
const pattern = /# APP CODE START(.*?)# APP CODE END/gs;
|
| 90 |
const template = `{html_template}`;
|
| 91 |
const completedTemplate = template.replace(pattern, code);
|
| 92 |
|
|
|
|
|
|
|
| 93 |
const oldFrame = document.querySelector('.my-frame');
|
| 94 |
oldFrame.remove();
|
| 95 |
|
|
|
|
| 104 |
frame.contentWindow.document.open('text/html', 'replace');
|
| 105 |
frame.contentWindow.document.write(completedTemplate);
|
| 106 |
frame.contentWindow.document.close();
|
|
|
|
| 107 |
}}"""
|
| 108 |
|
| 109 |
copy_snippet_js = f"""async (code) => {{
|
|
|
|
| 116 |
const completedTemplate = template.replace(pattern, code);
|
| 117 |
|
| 118 |
const snippet = completedTemplate;
|
|
|
|
|
|
|
| 119 |
await navigator.clipboard.writeText(snippet);
|
|
|
|
|
|
|
| 120 |
}}"""
|
| 121 |
|
| 122 |
download_code_js = f"""(code) => {{
|
|
|
|
| 123 |
const pattern = /# APP CODE START(.*?)# APP CODE END/gs;
|
| 124 |
const template = `{html_template}`;
|
| 125 |
// Step 1: Generate the HTML content
|
|
|
|
| 141 |
|
| 142 |
// Clean up by revoking the URL
|
| 143 |
URL.revokeObjectURL(url);
|
|
|
|
|
|
|
| 144 |
}}"""
|
| 145 |
|
| 146 |
|
|
|
|
| 150 |
|
| 151 |
|
| 152 |
def generate_text(code, prompt):
|
| 153 |
+
logger.info(f"Calling API with prompt:\n{prompt}")
|
| 154 |
prompt = f"```python\n{code}```\nGiven the code above return only updated code for the following request:\n{prompt}\n<|assistant|>"
|
| 155 |
params = {"max_new_tokens": 512}
|
| 156 |
output = query({
|
| 157 |
"inputs": prompt,
|
| 158 |
"parameters": params,
|
| 159 |
})
|
| 160 |
+
if 'error' in output:
|
| 161 |
+
logger.warning(f'Language model call failed: {output["error"]}')
|
| 162 |
+
raise gr.Warning(f'Language model call failed: {output["error"]}')
|
| 163 |
+
logger.info(f'API RESPONSE\n{output[0]["generated_text"]}')
|
| 164 |
assistant_reply = output[0]["generated_text"].split('<|assistant|>')[1]
|
| 165 |
match = re.search(code_pattern, assistant_reply, re.DOTALL)
|
| 166 |
new_code = match.group(1)
|
| 167 |
+
logger.info(f'NEW CODE:\nnew_code')
|
| 168 |
# TODO: error handling here
|
| 169 |
return assistant_reply, new_code, None
|
| 170 |
|
| 171 |
|
| 172 |
def transcribe(audio):
|
| 173 |
result = whisper_pipe(audio)
|
| 174 |
+
return result["text"], None
|
| 175 |
|
| 176 |
|
| 177 |
def copy_notify(code):
|
|
|
|
| 180 |
|
| 181 |
with gr.Blocks() as demo:
|
| 182 |
gr.Markdown("<h1 align=\"center\">KiteWind πͺπ</h1>")
|
| 183 |
+
gr.Markdown(
|
| 184 |
+
"<h4 align=\"center\">Chat-assisted web app creator by <a href=\"https://huggingface.co/gstaff\">@gstaff</a></h4>")
|
| 185 |
with gr.Row():
|
| 186 |
with gr.Column():
|
| 187 |
gr.Markdown("## 1. Run your app in the browser!")
|
|
|
|
| 196 |
out_text = gr.TextArea(label="Chat Assistant Response")
|
| 197 |
clear = gr.ClearButton([in_prompt, in_audio, out_text])
|
| 198 |
with gr.Column():
|
| 199 |
+
code_area = gr.Code(label="App Code - You can also edit directly and then click Update App",
|
| 200 |
+
language='python', value=starting_app_code)
|
| 201 |
update_btn = gr.Button("Update App", variant="primary")
|
| 202 |
+
code_update_params = {'fn': None, 'inputs': code_area, 'outputs': None, '_js': update_iframe_js}
|
| 203 |
+
gen_text_params = {'fn': generate_text, 'inputs': [code_area, in_prompt], 'outputs': [out_text, code_area]}
|
| 204 |
+
transcribe_params = {'fn': transcribe, 'inputs': [in_audio], 'outputs': [in_prompt, in_audio]}
|
| 205 |
+
update_btn.click(**code_update_params)
|
| 206 |
+
in_prompt.submit(**gen_text_params).then(**code_update_params)
|
| 207 |
+
in_audio.stop_recording(**transcribe_params).then(**gen_text_params).then(**code_update_params)
|
| 208 |
with gr.Row():
|
| 209 |
with gr.Column():
|
| 210 |
gr.Markdown("## 3. Export your app to share!")
|
|
|
|
| 216 |
with gr.Column():
|
| 217 |
gr.Markdown("## Current limitations")
|
| 218 |
with gr.Accordion("Click to view", open=False):
|
| 219 |
+
gr.Markdown(
|
| 220 |
+
"- Only gradio-lite apps using the python standard libraries and gradio are supported\n- The chat hasn't been tuned on gradio library data; it may make mistakes\n- The app needs to fully reload each time it is changed")
|
| 221 |
|
| 222 |
demo.load(None, None, None, _js=load_js)
|
| 223 |
demo.css = "footer {visibility: hidden}"
|