Update chatcode/app.py
Browse files- chatcode/app.py +45 -15
chatcode/app.py
CHANGED
|
@@ -4,6 +4,7 @@ import os
|
|
| 4 |
import threading
|
| 5 |
|
| 6 |
from .runtime import CONFIG, answer, gpu_available, load_engine
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
LOCK = threading.Lock()
|
|
@@ -31,28 +32,57 @@ def respond(message, history):
|
|
| 31 |
{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
with gr.Blocks(title=CONFIG["name"]) as demo:
|
| 35 |
-
gr.Markdown("# PocketVision · Chat &
|
| 36 |
if PREVIEW:
|
| 37 |
gr.Markdown("**Interface preview** · The cloud model is not connected yet.")
|
| 38 |
elif ADAPTER:
|
| 39 |
gr.Markdown("Custom chat and programming model · Review generated code before using it.")
|
| 40 |
else:
|
| 41 |
gr.Markdown("Pretrained model · Custom chat and programming training is pending.")
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
event
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def launch():
|
|
|
|
| 4 |
import threading
|
| 5 |
|
| 6 |
from .runtime import CONFIG, answer, gpu_available, load_engine
|
| 7 |
+
from .articles import MODES, POLICY_URL, article_messages, draft_article
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
LOCK = threading.Lock()
|
|
|
|
| 32 |
{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
|
| 33 |
|
| 34 |
|
| 35 |
+
def write_article(topic, rows, mode):
|
| 36 |
+
try:
|
| 37 |
+
article_messages(topic, rows, mode) # Reject incomplete packets before loading a model.
|
| 38 |
+
if PREVIEW:
|
| 39 |
+
raise ValueError("The cloud model is not connected yet.")
|
| 40 |
+
with LOCK:
|
| 41 |
+
tokenizer, model = engine()
|
| 42 |
+
rendered, _ = draft_article(tokenizer, model, topic, rows, mode)
|
| 43 |
+
return rendered, rendered
|
| 44 |
+
except (ValueError, RuntimeError) as error:
|
| 45 |
+
raise gr.Error(str(error)) from error
|
| 46 |
+
|
| 47 |
+
|
| 48 |
with gr.Blocks(title=CONFIG["name"]) as demo:
|
| 49 |
+
gr.Markdown("# PocketVision · Chat, Code & Writing\nTalk, work through bugs, and turn source notes into clear reference writing.")
|
| 50 |
if PREVIEW:
|
| 51 |
gr.Markdown("**Interface preview** · The cloud model is not connected yet.")
|
| 52 |
elif ADAPTER:
|
| 53 |
gr.Markdown("Custom chat and programming model · Review generated code before using it.")
|
| 54 |
else:
|
| 55 |
gr.Markdown("Pretrained model · Custom chat and programming training is pending.")
|
| 56 |
+
with gr.Tab("Chat & code"):
|
| 57 |
+
chatbot = gr.Chatbot(type="messages", label="Conversation", height=360, allow_tags=False)
|
| 58 |
+
message = gr.Textbox(label="Your message", lines=3, max_lines=14,
|
| 59 |
+
placeholder="Ask a question, paste code, or describe a bug…", interactive=not PREVIEW)
|
| 60 |
+
with gr.Row():
|
| 61 |
+
send = gr.Button("Send", variant="primary", interactive=not PREVIEW)
|
| 62 |
+
clear = gr.Button("New chat")
|
| 63 |
+
gr.Examples([
|
| 64 |
+
"Write a Python function that removes duplicates while preserving their order.",
|
| 65 |
+
"Explain JavaScript async and await with a small example.",
|
| 66 |
+
"Help me debug my code. I’ll paste it in the next message.",
|
| 67 |
+
], inputs=message)
|
| 68 |
+
for event in [send.click, message.submit]:
|
| 69 |
+
event(respond, [message, chatbot], [message, chatbot])
|
| 70 |
+
clear.click(lambda: ("", []), outputs=[message, chatbot])
|
| 71 |
+
with gr.Tab("Articles & research"):
|
| 72 |
+
gr.Markdown("Draft a short encyclopedia-style article from sources you have read, or prepare an outline for a human editor. "
|
| 73 |
+
"Paste relevant excerpts; this app does not open URLs or search the web.")
|
| 74 |
+
topic = gr.Textbox(label="Topic", placeholder="The subject you want to explain")
|
| 75 |
+
mode = gr.Radio(choices=list(MODES), value=MODES[0], label="Writing task")
|
| 76 |
+
gr.Markdown(f"For English Wikipedia, use the outline task: its [current guideline]({POLICY_URL}) "
|
| 77 |
+
"restricts AI-generated article prose. Reference articles here are for other writing contexts.")
|
| 78 |
+
sources = gr.Dataframe(headers=["Source title", "URL", "Relevant excerpt"],
|
| 79 |
+
datatype=["str", "str", "str"], row_count=(2, "dynamic"),
|
| 80 |
+
col_count=(3, "fixed"), type="array", wrap=True,
|
| 81 |
+
label="Sources", value=[["", "", ""], ["", "", ""]])
|
| 82 |
+
write = gr.Button("Prepare draft or outline", variant="primary", interactive=not PREVIEW)
|
| 83 |
+
draft = gr.Markdown()
|
| 84 |
+
copy = gr.Textbox(label="Markdown to copy", lines=8, show_copy_button=True, interactive=False)
|
| 85 |
+
write.click(write_article, [topic, sources, mode], [draft, copy])
|
| 86 |
|
| 87 |
|
| 88 |
def launch():
|