Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,15 @@
|
|
| 1 |
import os
|
| 2 |
-
import time
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
import gradio as gr
|
| 6 |
-
from threading import Thread
|
| 7 |
|
| 8 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 9 |
|
| 10 |
-
TITLE = "<h1><center>
|
| 11 |
|
| 12 |
PLACEHOLDER = """
|
| 13 |
<center>
|
| 14 |
-
<p>
|
| 15 |
</center>
|
| 16 |
"""
|
| 17 |
|
|
@@ -37,20 +35,9 @@ model = Transformer.from_folder(mistral_models_path)
|
|
| 37 |
|
| 38 |
|
| 39 |
@spaces.GPU()
|
| 40 |
-
def
|
| 41 |
-
message:
|
| 42 |
-
|
| 43 |
-
temperature: float = 0.3,
|
| 44 |
-
max_new_tokens: int = 1024,
|
| 45 |
-
):
|
| 46 |
-
print(f'message: {message}')
|
| 47 |
-
print(f'history: {history}')
|
| 48 |
-
|
| 49 |
-
conversation = []
|
| 50 |
-
for prompt, answer in history:
|
| 51 |
-
conversation.append(UserMessage(content=prompt))
|
| 52 |
-
conversation.append(AssistantMessage(content=answer))
|
| 53 |
-
conversation.append(UserMessage(content=message))
|
| 54 |
|
| 55 |
completion_request = ChatCompletionRequest(messages=conversation)
|
| 56 |
|
|
@@ -61,49 +48,31 @@ def stream_chat(
|
|
| 61 |
model,
|
| 62 |
max_tokens=max_new_tokens,
|
| 63 |
temperature=temperature,
|
| 64 |
-
eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id
|
|
|
|
| 65 |
|
| 66 |
result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
|
| 67 |
|
| 68 |
return result
|
| 69 |
-
|
| 70 |
-
chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
|
| 71 |
|
| 72 |
with gr.Blocks(theme="ocean") as demo:
|
| 73 |
gr.HTML(TITLE)
|
| 74 |
-
gr.
|
| 75 |
-
gr.
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
additional_inputs=[
|
| 81 |
-
gr.Slider(
|
| 82 |
-
minimum=0,
|
| 83 |
-
maximum=1,
|
| 84 |
-
step=0.1,
|
| 85 |
-
value=0.3,
|
| 86 |
-
label="Temperature",
|
| 87 |
-
render=False,
|
| 88 |
-
),
|
| 89 |
-
gr.Slider(
|
| 90 |
-
minimum=128,
|
| 91 |
-
maximum=8192,
|
| 92 |
-
step=1,
|
| 93 |
-
value=1024,
|
| 94 |
-
label="Max new tokens",
|
| 95 |
-
render=False,
|
| 96 |
-
),
|
| 97 |
-
],
|
| 98 |
-
examples=[
|
| 99 |
-
["Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option."],
|
| 100 |
-
["What are 5 creative things I could do with my kids' art? I don't want to throw them away, but it's also so much clutter."],
|
| 101 |
-
["Tell me a random fun fact about the Roman Empire."],
|
| 102 |
-
["Show me a code snippet of a website's sticky header in CSS and JavaScript."],
|
| 103 |
-
],
|
| 104 |
-
cache_examples=False,
|
| 105 |
-
)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 7 |
|
| 8 |
+
TITLE = "<h1><center>Learning Content Generator</center></h1>"
|
| 9 |
|
| 10 |
PLACEHOLDER = """
|
| 11 |
<center>
|
| 12 |
+
<p>Enter the topic, description, and difficulty level to generate learning content.</p>
|
| 13 |
</center>
|
| 14 |
"""
|
| 15 |
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
@spaces.GPU()
|
| 38 |
+
def generate_learning_content(topic: str, description: str, difficulty: str, temperature: float = 0.3, max_new_tokens: int = 1024):
|
| 39 |
+
message = f"Generate learning content on the topic '{topic}' with the description: '{description}' and difficulty level: '{difficulty}'. Provide the content in paragraph format."
|
| 40 |
+
conversation = [UserMessage(content=message)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
completion_request = ChatCompletionRequest(messages=conversation)
|
| 43 |
|
|
|
|
| 48 |
model,
|
| 49 |
max_tokens=max_new_tokens,
|
| 50 |
temperature=temperature,
|
| 51 |
+
eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id
|
| 52 |
+
)
|
| 53 |
|
| 54 |
result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
|
| 55 |
|
| 56 |
return result
|
|
|
|
|
|
|
| 57 |
|
| 58 |
with gr.Blocks(theme="ocean") as demo:
|
| 59 |
gr.HTML(TITLE)
|
| 60 |
+
topic_input = gr.Textbox(label="Topic", placeholder="Enter the topic for learning content.")
|
| 61 |
+
description_input = gr.Textbox(label="Description", placeholder="Enter a brief description of the topic.")
|
| 62 |
+
difficulty_input = gr.Textbox(label="Difficulty Level", placeholder="Enter the difficulty level (easy, medium, hard).")
|
| 63 |
+
|
| 64 |
+
temperature_slider = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.3, label="Temperature")
|
| 65 |
+
tokens_slider = gr.Slider(minimum=128, maximum=8192, step=1, value=1024, label="Max New Tokens")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
output = gr.Textbox(label="Generated Learning Content")
|
| 68 |
+
|
| 69 |
+
submit_button = gr.Button("Generate Content")
|
| 70 |
+
|
| 71 |
+
submit_button.click(
|
| 72 |
+
fn=generate_learning_content,
|
| 73 |
+
inputs=[topic_input, description_input, difficulty_input, temperature_slider, tokens_slider],
|
| 74 |
+
outputs=output,
|
| 75 |
+
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
demo.launch()
|