Create app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
message,
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from hf_model_adapter import HFLocalModelAdapter
|
| 3 |
|
| 4 |
+
# Load model once at startup
|
| 5 |
+
MODEL_NAME = "stabilityai/stablelm-3b-4e1t" # smaller, good for Spaces CPU/GPU
|
| 6 |
+
hf_adapter = HFLocalModelAdapter(model_name=MODEL_NAME)
|
| 7 |
|
| 8 |
+
def radio_agents_pipeline(user_message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
+
Simulates multi-agent flow:
|
| 11 |
+
1. Writer creates draft.
|
| 12 |
+
2. Editor polishes.
|
| 13 |
+
3. QA reviews.
|
| 14 |
"""
|
| 15 |
+
# Writer
|
| 16 |
+
writer_prompt = f"You are a radio script writer. Draft a short radio segment script based on: {user_message}"
|
| 17 |
+
writer_out = hf_adapter.generate(writer_prompt, max_new_tokens=400)
|
| 18 |
|
| 19 |
+
# Editor
|
| 20 |
+
editor_prompt = "You are an editor. Improve clarity, shorten sentences, and make it radio-friendly.\n\n" + writer_out
|
| 21 |
+
edited_out = hf_adapter.generate(editor_prompt, max_new_tokens=300)
|
| 22 |
|
| 23 |
+
# QA
|
| 24 |
+
qa_prompt = "You are a compliance QA. Check for unsafe, offensive, or disallowed content. Reply with 'OK' if fine, otherwise list issues.\n\n" + edited_out
|
| 25 |
+
qa_out = hf_adapter.generate(qa_prompt, max_new_tokens=200)
|
| 26 |
|
| 27 |
+
# Final script output
|
| 28 |
+
final_script = f"📜 **Draft:**\n{writer_out}\n\n✂️ **Edited:**\n{edited_out}\n\n✅ **QA Result:**\n{qa_out}"
|
| 29 |
+
return final_script
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("# 🎙️ AutoGen Radio Content Creator (Gradio + HF SLM)")
|
| 33 |
+
chatbot = gr.Chatbot(height=600)
|
| 34 |
+
msg = gr.Textbox(label="Enter your request (e.g., Morning show script about local events)")
|
| 35 |
+
clear = gr.Button("Clear Chat")
|
| 36 |
+
|
| 37 |
+
def respond(user_message, chat_history):
|
| 38 |
+
response = radio_agents_pipeline(user_message, chat_history)
|
| 39 |
+
chat_history.append((user_message, response))
|
| 40 |
+
return "", chat_history
|
| 41 |
|
| 42 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 43 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|