Spaces:
Runtime error
Runtime error
Create apps.py
Browse files
apps.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import spaces
|
| 6 |
+
import random
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
model_checkpoint = "HuggingFaceTB/SmolLM-360M"
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_checkpoint)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
|
| 12 |
+
|
| 13 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=2048, repetition_penalty=1.2, temperature=0.4)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
abs_path = Path(__file__).parent
|
| 17 |
+
|
| 18 |
+
df = pd.read_csv(str(abs_path / "models.csv"))
|
| 19 |
+
df.to_html("tab.html")
|
| 20 |
+
|
| 21 |
+
def refreshfn() -> gr.HTML:
|
| 22 |
+
df = pd.read_csv(str(abs_path / "models.csv"))
|
| 23 |
+
df.to_html("tab.html")
|
| 24 |
+
f = open("tab.html")
|
| 25 |
+
content = f.read()
|
| 26 |
+
f.close()
|
| 27 |
+
t = gr.HTML(content)
|
| 28 |
+
return t
|
| 29 |
+
|
| 30 |
+
def chatfn(text):
|
| 31 |
+
return text, text
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("""
|
| 35 |
+
# 🥇 Leaderboard Component
|
| 36 |
+
""")
|
| 37 |
+
with gr.Tabs():
|
| 38 |
+
with gr.Tab("Demo"):
|
| 39 |
+
f = open("tab.html")
|
| 40 |
+
content = f.read()
|
| 41 |
+
f.close()
|
| 42 |
+
t = gr.HTML(content)
|
| 43 |
+
btn = gr.Button("Refresh")
|
| 44 |
+
btn.click(fn=refreshfn, inputs=None, outputs=t)
|
| 45 |
+
with gr.Tab("Chats"):
|
| 46 |
+
import random
|
| 47 |
+
import time
|
| 48 |
+
with gr.Column():
|
| 49 |
+
chatbot = gr.Chatbot()
|
| 50 |
+
with gr.Column():
|
| 51 |
+
chatbot1 = gr.Chatbot()
|
| 52 |
+
msg = gr.Textbox()
|
| 53 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 54 |
+
@spaces.GPU(duration=120)
|
| 55 |
+
def respond(message, chat_history):
|
| 56 |
+
response = pipe(message)
|
| 57 |
+
bot_message = response[0]["generated_text"]
|
| 58 |
+
chat_history.append((message, bot_message))
|
| 59 |
+
return "", chat_history
|
| 60 |
+
|
| 61 |
+
import concurrent.futures
|
| 62 |
+
|
| 63 |
+
def run_functions_simultaneously():
|
| 64 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 65 |
+
# Submit the first function
|
| 66 |
+
future1 = executor.submit(msg.submit, respond, [msg, chatbot], [msg, chatbot])
|
| 67 |
+
|
| 68 |
+
# Submit the second function
|
| 69 |
+
future2 = executor.submit(msg.submit, respond, [msg, chatbot1], [msg, chatbot1])
|
| 70 |
+
|
| 71 |
+
# Wait for both futures to complete
|
| 72 |
+
concurrent.futures.wait([future1, future2])
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# Call the function to run the tasks simultaneously
|
| 76 |
+
run_functions_simultaneously()
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|