Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 6 |
+
MODEL = "microsoft/Phi-3-mini-128k-instruct"
|
| 7 |
+
|
| 8 |
+
# -------------------------
|
| 9 |
+
# Core function: call HF API
|
| 10 |
+
# -------------------------
|
| 11 |
+
def query_llm(tweet, mode):
|
| 12 |
+
prompt = f"Text: {tweet}\n\n"
|
| 13 |
+
if mode == "simple":
|
| 14 |
+
prompt += "Task: Is the above TEXT toxic? First clearly state your decision. Then, provide specific reason(s) for your decision. If there is more than one reason, provide them in a numbered list."
|
| 15 |
+
else:
|
| 16 |
+
prompt += "Task: Is the above TEXT toxic? First clearly state your decision. Then, provide specific reason(s) for your decision. If there is more than one reason, provide them in a numbered list. Your reason(s) must be non-redundant and jointly sufficient to justify your decision. In other words, there should not be any internal or external information unused in your explanation."
|
| 17 |
+
|
| 18 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 19 |
+
response = requests.post(
|
| 20 |
+
f"https://api-inference.huggingface.co/models/{MODEL}",
|
| 21 |
+
headers=headers,
|
| 22 |
+
json={"inputs": prompt}
|
| 23 |
+
)
|
| 24 |
+
try:
|
| 25 |
+
out = response.json()
|
| 26 |
+
return out[0]["generated_text"] if isinstance(out, list) else str(out)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {str(e)}"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# -------------------------
|
| 32 |
+
# Preloaded tweets
|
| 33 |
+
# -------------------------
|
| 34 |
+
preloaded_tweets = [
|
| 35 |
+
"People from the outside must look at us and think 'what stupid people, what are they doing?",
|
| 36 |
+
"Donald Trump was responding to comments from Prime Minister David Cameron, who called his proposal to temporarily ban all Muslims from the United States divisive, stupid and wrong.,
|
| 37 |
+
"Every binder should have a warning label on it that says CAUTION: BORING SHIT INSIDE.",
|
| 38 |
+
"If you want to call me a bitch, call me a bitch, but that's just kind of stupid",
|
| 39 |
+
"I want to just have laughs on the internet, not have fucking twelve year olds intruding into my personal life.",
|
| 40 |
+
"Today I sit down with him to talk about his forthcoming book Writing Without Bullshit: Boost Your Career by Saying What You Mean."
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# -------------------------
|
| 45 |
+
# Gradio Blocks UI
|
| 46 |
+
# -------------------------
|
| 47 |
+
with gr.Blocks(title="Tweet Explainer") as demo:
|
| 48 |
+
gr.Markdown("## 🐦 Tweet Explainer")
|
| 49 |
+
gr.Markdown("Select a tweet from the list or enter your own, then view two explanations on the right.")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
# Left pane
|
| 53 |
+
with gr.Column(scale=1):
|
| 54 |
+
gr.Markdown("### Select or Enter a Tweet")
|
| 55 |
+
|
| 56 |
+
tweet_list = gr.Dropdown(preloaded_tweets, label="Choose a tweet")
|
| 57 |
+
custom_tweet = gr.Textbox(
|
| 58 |
+
placeholder="Or enter your own tweet here...",
|
| 59 |
+
lines=3,
|
| 60 |
+
label="Custom Tweet"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Button to confirm selection
|
| 64 |
+
submit_btn = gr.Button("Use this Tweet")
|
| 65 |
+
|
| 66 |
+
# Right pane
|
| 67 |
+
with gr.Column(scale=2):
|
| 68 |
+
gr.Markdown("### Explanations")
|
| 69 |
+
|
| 70 |
+
with gr.Tab("Simple Explanation"):
|
| 71 |
+
simple_output = gr.Textbox(label="Simple", lines=5)
|
| 72 |
+
|
| 73 |
+
with gr.Tab("Detailed Explanation"):
|
| 74 |
+
detailed_output = gr.Textbox(label="Detailed", lines=8)
|
| 75 |
+
|
| 76 |
+
# -------------------------
|
| 77 |
+
# Logic: when user clicks button
|
| 78 |
+
# -------------------------
|
| 79 |
+
def process_tweet(selected, custom):
|
| 80 |
+
tweet = custom.strip() if custom else selected
|
| 81 |
+
if not tweet:
|
| 82 |
+
return "Please enter or select a tweet!", "Please enter or select a tweet!"
|
| 83 |
+
|
| 84 |
+
simple = query_llm(tweet, "simple")
|
| 85 |
+
detailed = query_llm(tweet, "detailed")
|
| 86 |
+
return simple, detailed
|
| 87 |
+
|
| 88 |
+
submit_btn.click(
|
| 89 |
+
process_tweet,
|
| 90 |
+
inputs=[tweet_list, custom_tweet],
|
| 91 |
+
outputs=[simple_output, detailed_output]
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# Run app
|
| 96 |
+
demo.launch()
|