awacke1 commited on
Commit
0ee709a
·
1 Parent(s): 7d0783c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import asyncio
3
+ from concurrent.futures import ThreadPoolExecutor
4
+ import requests
5
+ import gradio as gr
6
+
7
+ MAX_NEW_TOKENS = 256
8
+ TOKEN = os.environ.get("HF_TOKEN", None)
9
+ URLS = [
10
+ "https://api-inference.huggingface.co/models/google/flan-ul2",
11
+ "https://api-inference.huggingface.co/models/google/flan-t5-xxl",
12
+ ]
13
+
14
+ def fetch(session, text, api_url):
15
+ model = api_url.split("/")[-1]
16
+ response = session.post(api_url, json={"inputs": text, "parameters": {"max_new_tokens": MAX_NEW_TOKENS}})
17
+ if response.status_code != 200:
18
+ return model, None
19
+ return model, response.json()
20
+
21
+ async def inference(text):
22
+ with ThreadPoolExecutor(max_workers=2) as executor:
23
+ with requests.Session() as session:
24
+ session.headers = {"Authorization": f"Bearer {TOKEN}"}
25
+ # Initialize the event loop
26
+ loop = asyncio.get_event_loop()
27
+ tasks = [
28
+ loop.run_in_executor(
29
+ executor, fetch, *(session, text, url) # Allows us to pass in multiple arguments to `fetch`
30
+ )
31
+ for url in URLS
32
+ ]
33
+
34
+ # Initializes the tasks to run and awaits their results
35
+ responses = [None, None]
36
+ for (model, response) in await asyncio.gather(*tasks):
37
+ if response is not None:
38
+ if model == "flan-ul2":
39
+ responses[0] = response[0]["generated_text"]
40
+ elif model == "flan-t5-xxl":
41
+ responses[1] = response[0]["generated_text"]
42
+ return responses
43
+
44
+ def feedback(inputs, feedback, is_positive):
45
+ with open('promptlog.txt', 'a') as f:
46
+ f.write(f"Inputs: {inputs}\nFeedback: {feedback}\nIs positive: {is_positive}\n\n")
47
+
48
+ def display_history():
49
+ try:
50
+ with open('promptlog.txt', 'r') as f:
51
+ history = f.read()
52
+ except FileNotFoundError:
53
+ history = "No history yet."
54
+ print(history)
55
+
56
+ def app():
57
+ title = "Flan UL2 vs Flan T5 XXL"
58
+ description = "Compare with feedback: [Flan-T5-xxl](https://huggingface.co/google/flan-t5-xxl) and [Flan-UL2](https://huggingface.co/google/flan-ul2)."
59
+ inputs = gr.inputs.Textbox(lines=3, label="Input Prompt")
60
+ outputs = [gr.outputs.Textbox(lines=3, label="Flan T5-UL2"), gr.outputs.Textbox(lines=3, label="Flan T5-XXL")]
61
+ feedback_box = gr.inputs.CheckboxGroup(["Positive feedback", "Negative feedback"], label="Feedback")
62
+ feedback_text = gr.inputs.Textbox(label="Feedback Reason")
63
+ feedback_button = gr.inputs.Button(label="Submit Feedback")
64
+ display_history_button = gr.inputs.Button(label="Display Feedback History")
65
+
66
+ def predict_text(inputs):
67
+ return inference(inputs)
68
+
69
+ def handle_feedback(inputs, feedback, is_positive):
70
+ feedback(inputs, feedback, is_positive)
71
+ return "Thank you for your feedback!"
72
+
73
+ def handle_display_history():
74
+ display_history()
75
+
76
+ gr.Interface(fn=predict_text, inputs=inputs, outputs=outputs, title=title, description=description).launch()
77
+
78
+ feedback_ui = gr.Interface(fn=handle_feedback, inputs=[inputs, feedback_box, feedback_text, feedback_button], outputs=gr.outputs