Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
# Using the exact model you specified
|
| 7 |
+
MODEL_ID = "google/gemma-3n-e4b-it"
|
| 8 |
+
|
| 9 |
+
client = InferenceClient(api_key=os.environ.get("HF_TOKEN"))
|
| 10 |
+
|
| 11 |
+
def generate(current_text, tone, length, context, recent_steps):
|
| 12 |
+
prompt = f"""
|
| 13 |
+
You are a precise senior copy editor. Rewrite the provided copy using the requested controls.
|
| 14 |
+
Create exactly three meaningfully different options for the user to choose from.
|
| 15 |
+
Each option must follow the same selected tone and length.
|
| 16 |
+
Return valid JSON exactly like this: {{"options":["first rewrite","second rewrite","third rewrite"]}}
|
| 17 |
+
|
| 18 |
+
Tone: {tone}
|
| 19 |
+
Length: {length}
|
| 20 |
+
Context: {context}
|
| 21 |
+
Recent Steps: {recent_steps}
|
| 22 |
+
|
| 23 |
+
Current copy:
|
| 24 |
+
{current_text}
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
completion = client.chat_completion(
|
| 29 |
+
model=MODEL_ID,
|
| 30 |
+
messages=[
|
| 31 |
+
{"role": "system", "content": "Return valid JSON only. No commentary."},
|
| 32 |
+
{"role": "user", "content": prompt}
|
| 33 |
+
],
|
| 34 |
+
max_tokens=500,
|
| 35 |
+
temperature=0.7
|
| 36 |
+
)
|
| 37 |
+
return completion.choices[0].message.content
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return json.dumps({"error": str(e)})
|
| 40 |
+
|
| 41 |
+
# Expose as a Gradio API
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=generate,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Textbox(label="current_text"),
|
| 46 |
+
gr.Textbox(label="tone"),
|
| 47 |
+
gr.Textbox(label="length"),
|
| 48 |
+
gr.Textbox(label="context"),
|
| 49 |
+
gr.Textbox(label="recent_steps")
|
| 50 |
+
],
|
| 51 |
+
outputs=gr.JSON(),
|
| 52 |
+
api_name="predict"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
demo.launch()
|