Spaces:
Running
Running
qmicon
commited on
Commit
·
043e823
1
Parent(s):
6b3f5c2
added download button
Browse files- app.py +83 -1
- requirements.txt +75 -2
app.py
CHANGED
|
@@ -2,6 +2,9 @@ from openai import OpenAI
|
|
| 2 |
import gradio as gr
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
# Get the API key from the environment variable
|
|
@@ -31,4 +34,83 @@ def predict(message, history):
|
|
| 31 |
partial_message = partial_message + chunk.choices[0].delta.content
|
| 32 |
yield partial_message
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
| 5 |
+
from html.entities import codepoint2name
|
| 6 |
+
import tempfile
|
| 7 |
+
import markdown2
|
| 8 |
|
| 9 |
|
| 10 |
# Get the API key from the environment variable
|
|
|
|
| 34 |
partial_message = partial_message + chunk.choices[0].delta.content
|
| 35 |
yield partial_message
|
| 36 |
|
| 37 |
+
|
| 38 |
+
def convert_special_chars(text):
|
| 39 |
+
return ''.join(f'&{codepoint2name[ord(char)]};' if ord(char) in codepoint2name else char for char in text)
|
| 40 |
+
|
| 41 |
+
def download_chat_history(history):
|
| 42 |
+
chat_data = []
|
| 43 |
+
for human, assistance in history:
|
| 44 |
+
chat_data.append({"sender": "User", "content": human})
|
| 45 |
+
chat_data.append({"sender": "AI", "content": assistance})
|
| 46 |
+
html_content = """
|
| 47 |
+
<!DOCTYPE html>
|
| 48 |
+
<html lang="en">
|
| 49 |
+
<head>
|
| 50 |
+
<meta charset="UTF-8">
|
| 51 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 52 |
+
<title>NoCodeGenie Conversation</title>
|
| 53 |
+
<style>
|
| 54 |
+
body {
|
| 55 |
+
font-family: Arial, sans-serif;
|
| 56 |
+
margin: 0;
|
| 57 |
+
padding: 20px;
|
| 58 |
+
background-color: #f5f5f5;
|
| 59 |
+
}
|
| 60 |
+
.chat-container {
|
| 61 |
+
width: 60%;
|
| 62 |
+
margin: 20px auto;
|
| 63 |
+
padding: 20px;
|
| 64 |
+
background-color: #ffffff;
|
| 65 |
+
border: 1px solid #ddd;
|
| 66 |
+
border-radius: 10px;
|
| 67 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
| 68 |
+
}
|
| 69 |
+
.message {
|
| 70 |
+
padding: 15px;
|
| 71 |
+
margin-bottom: 10px;
|
| 72 |
+
border-radius: 8px;
|
| 73 |
+
display: flex;
|
| 74 |
+
flex-direction: column;
|
| 75 |
+
}
|
| 76 |
+
.user {
|
| 77 |
+
background-color: #e1f5fe;
|
| 78 |
+
align-self: flex-start;
|
| 79 |
+
}
|
| 80 |
+
.bot {
|
| 81 |
+
background-color: #ffe0b2;
|
| 82 |
+
align-self: flex-end;
|
| 83 |
+
}
|
| 84 |
+
.sender {
|
| 85 |
+
font-weight: bold;
|
| 86 |
+
margin-bottom: 5px;
|
| 87 |
+
font-size: 1.4em;
|
| 88 |
+
}
|
| 89 |
+
.content {
|
| 90 |
+
font-size: 1em;
|
| 91 |
+
line-height: 1.5;
|
| 92 |
+
}
|
| 93 |
+
</style>
|
| 94 |
+
</head>
|
| 95 |
+
<body>
|
| 96 |
+
<div class="chat-container">
|
| 97 |
+
"""
|
| 98 |
+
for message in chat_data:
|
| 99 |
+
class_name = "user" if message["sender"] == "User" else "bot"
|
| 100 |
+
content_html = markdown2.markdown(message["content"])
|
| 101 |
+
html_content += f'<div class="message {class_name}"><span class="sender">{message["sender"]}:</span><span class="content">{content_html}</span></div>\n'
|
| 102 |
+
|
| 103 |
+
# Format the complete HTML with messages
|
| 104 |
+
html_content += "</div></body></html>"
|
| 105 |
+
|
| 106 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8") as temp_file:
|
| 107 |
+
temp_file.write(html_content)
|
| 108 |
+
return temp_file.name
|
| 109 |
+
|
| 110 |
+
chatbot = gr.Chatbot()
|
| 111 |
+
with gr.Blocks() as demo:
|
| 112 |
+
gr.ChatInterface(fn=predict, title="NoCodeGenie: bubble.io simplified", chatbot=chatbot)
|
| 113 |
+
download_button = gr.Button("Download Chat History")
|
| 114 |
+
download_button.click(download_chat_history, inputs=[chatbot], outputs=gr.File())
|
| 115 |
+
|
| 116 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,75 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
altair==5.3.0
|
| 3 |
+
annotated-types==0.7.0
|
| 4 |
+
anyio==4.4.0
|
| 5 |
+
attrs==23.2.0
|
| 6 |
+
certifi==2024.6.2
|
| 7 |
+
charset-normalizer==3.3.2
|
| 8 |
+
click==8.1.7
|
| 9 |
+
colorama==0.4.6
|
| 10 |
+
contourpy==1.2.1
|
| 11 |
+
cycler==0.12.1
|
| 12 |
+
distro==1.9.0
|
| 13 |
+
dnspython==2.6.1
|
| 14 |
+
email_validator==2.2.0
|
| 15 |
+
fastapi==0.111.0
|
| 16 |
+
fastapi-cli==0.0.4
|
| 17 |
+
ffmpy==0.3.2
|
| 18 |
+
filelock==3.15.4
|
| 19 |
+
fonttools==4.53.0
|
| 20 |
+
fsspec==2024.6.0
|
| 21 |
+
gradio==4.37.1
|
| 22 |
+
gradio_client==1.0.2
|
| 23 |
+
h11==0.14.0
|
| 24 |
+
httpcore==1.0.5
|
| 25 |
+
httptools==0.6.1
|
| 26 |
+
httpx==0.27.0
|
| 27 |
+
huggingface-hub==0.23.4
|
| 28 |
+
idna==3.7
|
| 29 |
+
importlib_resources==6.4.0
|
| 30 |
+
Jinja2==3.1.4
|
| 31 |
+
jsonschema==4.22.0
|
| 32 |
+
jsonschema-specifications==2023.12.1
|
| 33 |
+
kiwisolver==1.4.5
|
| 34 |
+
markdown-it-py==3.0.0
|
| 35 |
+
markdown2==2.4.13
|
| 36 |
+
MarkupSafe==2.1.5
|
| 37 |
+
matplotlib==3.9.0
|
| 38 |
+
mdurl==0.1.2
|
| 39 |
+
numpy==2.0.0
|
| 40 |
+
openai==1.35.5
|
| 41 |
+
orjson==3.10.5
|
| 42 |
+
packaging==24.1
|
| 43 |
+
pandas==2.2.2
|
| 44 |
+
pillow==10.3.0
|
| 45 |
+
pydantic==2.7.4
|
| 46 |
+
pydantic_core==2.18.4
|
| 47 |
+
pydub==0.25.1
|
| 48 |
+
Pygments==2.18.0
|
| 49 |
+
pyparsing==3.1.2
|
| 50 |
+
python-dateutil==2.9.0.post0
|
| 51 |
+
python-dotenv==1.0.1
|
| 52 |
+
python-multipart==0.0.9
|
| 53 |
+
pytz==2024.1
|
| 54 |
+
PyYAML==6.0.1
|
| 55 |
+
referencing==0.35.1
|
| 56 |
+
requests==2.32.3
|
| 57 |
+
rich==13.7.1
|
| 58 |
+
rpds-py==0.18.1
|
| 59 |
+
ruff==0.4.10
|
| 60 |
+
semantic-version==2.10.0
|
| 61 |
+
shellingham==1.5.4
|
| 62 |
+
six==1.16.0
|
| 63 |
+
sniffio==1.3.1
|
| 64 |
+
starlette==0.37.2
|
| 65 |
+
tomlkit==0.12.0
|
| 66 |
+
toolz==0.12.1
|
| 67 |
+
tqdm==4.66.4
|
| 68 |
+
typer==0.12.3
|
| 69 |
+
typing_extensions==4.12.2
|
| 70 |
+
tzdata==2024.1
|
| 71 |
+
ujson==5.10.0
|
| 72 |
+
urllib3==2.2.2
|
| 73 |
+
uvicorn==0.30.1
|
| 74 |
+
watchfiles==0.22.0
|
| 75 |
+
websockets==11.0.3
|