Spaces:
Runtime error
Runtime error
import os | |
import asyncio | |
from concurrent.futures import ThreadPoolExecutor | |
import requests | |
import gradio as gr | |
MAX_NEW_TOKENS = 256 | |
TOKEN = os.environ.get("HF_TOKEN", None) | |
URLS = [ | |
"https://api-inference.huggingface.co/models/google/flan-ul2", | |
"https://api-inference.huggingface.co/models/google/flan-t5-xxl", | |
] | |
def fetch(session, text, api_url): | |
model = api_url.split("/")[-1] | |
response = session.post(api_url, json={"inputs": text, "parameters": {"max_new_tokens": MAX_NEW_TOKENS}}) | |
if response.status_code != 200: | |
return model, None | |
return model, response.json() | |
async def inference(text): | |
with ThreadPoolExecutor(max_workers=2) as executor: | |
with requests.Session() as session: | |
session.headers = {"Authorization": f"Bearer {TOKEN}"} | |
# Initialize the event loop | |
loop = asyncio.get_event_loop() | |
tasks = [ | |
loop.run_in_executor( | |
executor, fetch, *(session, text, url) # Allows us to pass in multiple arguments to `fetch` | |
) | |
for url in URLS | |
] | |
# Initializes the tasks to run and awaits their results | |
responses = [None, None] | |
for (model, response) in await asyncio.gather(*tasks): | |
if response is not None: | |
if model == "flan-ul2": | |
responses[0] = response[0]["generated_text"] | |
elif model == "flan-t5-xxl": | |
responses[1] = response[0]["generated_text"] | |
return responses | |
def feedback(inputs, feedback, is_positive): | |
with open('promptlog.txt', 'a') as f: | |
f.write(f"Inputs: {inputs}\nFeedback: {feedback}\nIs positive: {is_positive}\n\n") | |
def display_history(): | |
try: | |
with open('promptlog.txt', 'r') as f: | |
history = f.read() | |
except FileNotFoundError: | |
history = "No history yet." | |
print(history) | |
def app(): | |
title = "Flan UL2 vs Flan T5 XXL" | |
description = "Compare with feedback: [Flan-T5-xxl](https://huggingface.co/google/flan-t5-xxl) and [Flan-UL2](https://huggingface.co/google/flan-ul2)." | |
inputs = gr.inputs.Textbox(lines=3, label="Input Prompt") | |
outputs = [gr.outputs.Textbox(lines=3, label="Flan T5-UL2"), gr.outputs.Textbox(lines=3, label="Flan T5-XXL")] | |
feedback_box = gr.inputs.CheckboxGroup(["Positive feedback", "Negative feedback"], label="Feedback") | |
feedback_text = gr.inputs.Textbox(label="Feedback Reason") | |
feedback_button = gr.inputs.Button(label="Submit Feedback") | |
display_history_button = gr.inputs.Button(label="Display Feedback History") | |
def predict_text(inputs): | |
return inference(inputs) | |
def handle_feedback(inputs, feedback, is_positive): | |
feedback(inputs, feedback, is_positive) | |
return "Thank you for your feedback!" | |
def handle_display_history(): | |
display_history() | |
gr.Interface(fn=predict_text, inputs=inputs, outputs=outputs, title=title, description=description).launch() | |
feedback_ui = gr.Interface(fn=handle_feedback, inputs=[inputs, feedback_box, feedback_text, feedback_button], outputs=gr.outputs.Textbox(label="Feedback Submitted"), title="Feedback", description="Please provide feedback on the model's response.") | |
display_history_ui = gr.Interface(fn=handle_display_history, inputs=display_history_button, outputs=gr.outputs.Textbox(label="Feedback History"), title="Feedback History", description="View history of feedback submissions.") | |
gr.Interface([feedback_ui, display_history_ui], columns=2, title="Flan Feedback").launch() | |
if name == 'main': | |
app() |