abhiimanyu commited on
Commit
520db8f
·
verified ·
1 Parent(s): dd85892

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -54
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>Mistral-lab</center></h1>"
11
 
12
  PLACEHOLDER = """
13
  <center>
14
- <p>Chat with Mistral AI LLM.</p>
15
  </center>
16
  """
17
 
@@ -37,20 +35,9 @@ model = Transformer.from_folder(mistral_models_path)
37
 
38
 
39
  @spaces.GPU()
40
- def stream_chat(
41
- message: str,
42
- history: list,
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.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
75
- gr.ChatInterface(
76
- fn=stream_chat,
77
- chatbot=chatbot,
78
- fill_height=True,
79
- additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
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()