Ramaravind's picture
Update app.py
ea6b076 verified
raw
history blame
3.88 kB
import gradio as gr
import requests
import os
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
MODEL = "bigscience/bloom-560m" #"microsoft/Phi-3-mini-128k-instruct"
# -------------------------
# Core function: call HF API
# -------------------------
def query_llm(tweet, mode):
prompt = f"Text: {tweet}\n\n"
if mode == "simple":
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."
else:
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."
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
response = requests.post(
f"https://api-inference.huggingface.co/models/{MODEL}",
headers=headers,
json={"inputs": prompt}
)
try:
out = response.json()
return out[0]["generated_text"] if isinstance(out, list) else str(out)
except Exception as e:
return f"Error: {str(e)}"
# -------------------------
# Preloaded tweets
# -------------------------
preloaded_tweets = [
"People from the outside must look at us and think what stupid people, what are they doing?",
"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.",
"Every binder should have a warning label on it that says CAUTION: BORING SHIT INSIDE.",
"If you want to call me a bitch, call me a bitch, but that's just kind of stupid",
"I want to just have laughs on the internet, not have fucking twelve year olds intruding into my personal life.",
"Today I sit down with him to talk about his forthcoming book Writing Without Bullshit: Boost Your Career by Saying What You Mean."
]
# -------------------------
# Gradio Blocks UI
# -------------------------
with gr.Blocks(title="Tweet Explainer") as demo:
gr.Markdown("## 🐦 Tweet Explainer")
gr.Markdown("Select a tweet from the list or enter your own, then view two explanations on the right.")
with gr.Row():
# Left pane
with gr.Column(scale=1):
gr.Markdown("### Select or Enter a Tweet")
tweet_list = gr.Dropdown(preloaded_tweets, label="Choose a tweet")
custom_tweet = gr.Textbox(
placeholder="Or enter your own tweet here...",
lines=3,
label="Custom Tweet"
)
# Button to confirm selection
submit_btn = gr.Button("Use this Tweet")
# Right pane
with gr.Column(scale=2):
gr.Markdown("### Explanations")
with gr.Tab("Simple Explanation"):
simple_output = gr.Textbox(label="Simple", lines=5)
with gr.Tab("Detailed Explanation"):
detailed_output = gr.Textbox(label="Detailed", lines=8)
# -------------------------
# Logic: when user clicks button
# -------------------------
def process_tweet(selected, custom):
tweet = custom.strip() if custom else selected
if not tweet:
return "Please enter or select a tweet!", "Please enter or select a tweet!"
simple = query_llm(tweet, "simple")
detailed = query_llm(tweet, "detailed")
return simple, detailed
submit_btn.click(
process_tweet,
inputs=[tweet_list, custom_tweet],
outputs=[simple_output, detailed_output]
)
# Run app
demo.launch()