joys631 commited on
Commit
b29df2e
1 Parent(s): 0383393

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -39
app.py CHANGED
@@ -1,41 +1,52 @@
1
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
2
  import gradio as gr
3
 
4
- MODEL_NAME = "allenai/cosmo-xl"
5
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
6
- model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
7
-
8
-
9
- def generate_text(situation, instructions, prompt):
10
- """
11
- Generate text using the specified model and inputs.
12
-
13
- Args:
14
- situation: A short description of the context or situation.
15
- instructions: Specific instructions or constraints for text generation.
16
- prompt: The initial text prompt for the model to start from.
17
-
18
- Returns:
19
- The generated text.
20
- """
21
- inputs = tokenizer([situation, instructions, prompt], return_tensors="pt")
22
- outputs = model.generate(**inputs)
23
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
- return generated_text
25
-
26
-
27
- interface = gr.Interface(
28
- generate_text,
29
- [
30
- gr.Textbox(label="Situation"),
31
- gr.Textbox(label="Instructions"),
32
- gr.Textbox(label="Prompt"),
33
- ],
34
- "textbox",
35
- theme="huggingface",
36
- title="Cosmopolitan Conversationalist",
37
- description="Generate creative text with context and instructions!",
38
- )
39
-
40
- interface.launch(server_name="Cosmopolitan_Conversationalist")
41
-
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
  import gradio as gr
3
 
4
+ client = InferenceClient("allenai/cosmo-xl")
5
+
6
+ def format_prompt(message, history):
7
+ prompt = "<s>"
8
+ for user_prompt, bot_response in history:
9
+ prompt += f"[INST] {user_prompt} [/INST]"
10
+ prompt += f" {bot_response}</s> "
11
+ prompt += f"[INST] {message} [/INST]"
12
+ return prompt
13
+
14
+ def generate(
15
+ prompt, history, temperature=1.2, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0,
16
+ ):
17
+ temperature = float(temperature)
18
+ if temperature < 1e-2:
19
+ temperature = 1e-2
20
+ top_p = float(top_p)
21
+
22
+ generate_kwargs = dict(
23
+ temperature=temperature,
24
+ max_new_tokens=max_new_tokens,
25
+ top_p=top_p,
26
+ repetition_penalty=repetition_penalty,
27
+ do_sample=True,
28
+ seed=42,
29
+ )
30
+
31
+ formatted_prompt = format_prompt(prompt, history)
32
+
33
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
34
+ output = ""
35
+
36
+ for response in stream:
37
+ output += response.token.text
38
+ yield output
39
+ return output
40
+
41
+
42
+ mychatbot = gr.Chatbot(
43
+ avatar_images=["./user.png", "./user.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
44
+
45
+ demo = gr.ChatInterface(fn=generate,
46
+ chatbot=mychatbot,
47
+ title="fast ai aaaaa!!",
48
+ retry_btn="Regenerate",
49
+ undo_btn="Undo"
50
+ )
51
+
52
+ demo.queue().launch(show_api=True)