Sugamdeol commited on
Commit
1ed4c00
·
verified ·
1 Parent(s): 9d7d500

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+
4
+ # Set up the client for Mistral model inference
5
+ client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
6
+
7
+ # Function to format the conversation history
8
+ def format_prompt(message, history):
9
+ prompt = "<s>" # Begin with the start token
10
+ for user_prompt, bot_response in history:
11
+ # Append each turn of user-bot interaction to the prompt
12
+ prompt += f"[INST] {user_prompt} [/INST] {bot_response}</s> "
13
+ prompt += f"[INST] {message} [/INST]" # Add the latest user message
14
+ return prompt
15
+
16
+ # Text generation function with parameters
17
+ def generate(
18
+ prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
19
+ ):
20
+ # Ensure temperature and top_p are correctly set
21
+ temperature = max(float(temperature), 1e-2) # Prevent temperature going below 0.01
22
+ top_p = float(top_p)
23
+
24
+ # Keyword arguments for generation configuration
25
+ generate_kwargs = dict(
26
+ temperature=temperature,
27
+ max_new_tokens=max_new_tokens,
28
+ top_p=top_p,
29
+ repetition_penalty=repetition_penalty,
30
+ do_sample=True,
31
+ seed=42, # Ensures results are reproducible
32
+ )
33
+
34
+ # Format the prompt with the user's message and history
35
+ formatted_prompt = format_prompt(prompt, history)
36
+
37
+ # Call the text generation endpoint
38
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
39
+ output = "" # Initialize an empty string for the output
40
+
41
+ # Stream the response token by token
42
+ for response in stream:
43
+ output += response.token.text # Append the generated tokens to output
44
+ yield output # Yield partial output for real-time display
45
+ return output
46
+
47
+ # Additional inputs (sliders) for controlling generation parameters
48
+ additional_inputs=[
49
+ gr.Slider(
50
+ label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05,
51
+ interactive=True, info="Higher values produce more diverse outputs"
52
+ ),
53
+ gr.Slider(
54
+ label="Max new tokens", value=256, minimum=0, maximum=1048, step=64,
55
+ interactive=True, info="The maximum numbers of new tokens"
56
+ ),
57
+ gr.Slider(
58
+ label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1.0, step=0.05,
59
+ interactive=True, info="Higher values sample more low-probability tokens"
60
+ ),
61
+ gr.Slider(
62
+ label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05,
63
+ interactive=True, info="Penalize repeated tokens"
64
+ )
65
+ ]
66
+
67
+ # Gradio Chat Interface for the chatbot
68
+ gr.ChatInterface(
69
+ fn=generate, # The generate function is called when the user submits input
70
+ chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
71
+ additional_inputs=additional_inputs, # Sliders for adjusting generation parameters
72
+ title="Mistral 7B v0.3 ChatGPT Clone", # Title for the interface
73
+ description="A ChatGPT clone using Mistral 7B model. Adjust parameters to fine-tune the generation."
74
+ ).launch(show_api=False) # Launch the interface without showing the API key