ZoroaStrella commited on
Commit
ce9b3a4
·
1 Parent(s): cdceb5d

Update to use reka

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +156 -51
  3. requirements.txt +1 -0
README.md CHANGED
@@ -11,4 +11,4 @@ license: apache-2.0
11
  short_description: A chat interface to use Reka Flash 3 OSS apache model
12
  ---
13
 
14
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
11
  short_description: A chat interface to use Reka Flash 3 OSS apache model
12
  ---
13
 
14
+ An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
app.py CHANGED
@@ -1,64 +1,169 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
9
 
10
- def respond(
11
  message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
  temperature,
16
  top_p,
 
 
 
 
 
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Configuration
5
+ MODEL_NAME = "RekaAI/reka-flash-3"
6
+ DEFAULT_MAX_LENGTH = 1024
7
+ DEFAULT_TEMPERATURE = 0.7
8
 
9
+ # System prompt
10
+ SYSTEM_PROMPT = """You are Reka Flash-3, a helpful AI assistant created by Reka AI.
11
+ Provide detailed, helpful answers while maintaining safety.
12
+ Format responses clearly using markdown when appropriate."""
13
 
14
+ def generate_response(
15
  message,
16
+ chat_history,
17
+ system_prompt,
18
+ max_length,
19
  temperature,
20
  top_p,
21
+ top_k,
22
+ repetition_penalty,
23
+ presence_penalty,
24
+ frequency_penalty,
25
+ show_reasoning
26
  ):
27
+ # Format the prompt
28
+ formatted_prompt = f"System: {system_prompt}\n\nUser: {message}\n\nAssistant:"
29
 
30
+ # Create client
31
+ client = InferenceClient()
 
 
 
32
 
33
+ # Generate response
34
+ response = client.text_generation(
35
+ MODEL_NAME,
36
+ prompt=formatted_prompt,
37
+ max_new_tokens=max_length,
 
 
 
38
  temperature=temperature,
39
  top_p=top_p,
40
+ top_k=top_k,
41
+ repetition_penalty=repetition_penalty,
42
+ presence_penalty=presence_penalty,
43
+ frequency_penalty=frequency_penalty,
44
+ details=show_reasoning,
45
+ )
46
+
47
+ # Extract reasoning and final answer if available
48
+ reasoning = ""
49
+ final_answer = response
50
+ if show_reasoning and hasattr(response, 'details'):
51
+ reasoning = response.details.get('reasoning', '')
52
+ final_answer = response.generated_text
53
+
54
+ # Update chat history
55
+ chat_history.append((message, final_answer))
56
+
57
+ # Create full history with reasoning
58
+ full_history = list(chat_history)
59
+ if show_reasoning and reasoning:
60
+ full_history[-1] = (full_history[-1][0], f"{final_answer}\n\nREASONING:\n{reasoning}")
61
+
62
+ return "", chat_history, reasoning if show_reasoning else ""
63
+
64
+ # UI Components
65
+ with gr.Blocks(title="Reka Flash-3 Chat Demo", theme=gr.themes.Soft()) as demo:
66
+ # Header Section
67
+ gr.Markdown(f"""
68
+ # Reka Flash-3 Chat Interface
69
+ *Powered by [Reka Core AI](https://www.reka.ai/)*
70
+ """)
71
+
72
+ # Deployment Notice
73
+ with gr.Accordion("Important Deployment Notice", open=True):
74
+ gr.Markdown(f"""
75
+ **To deploy this model on Hugging Face Spaces:**
76
+ 1. Request access to Reka Flash-3 from [Hugging Face Hub](https://huggingface.co/{MODEL_NAME})
77
+ 2. Ensure you have Hugging Face PRO subscription
78
+ 3. Add your HF token in Space settings
79
+ 4. Set `GPU_SMALL` or higher in Space hardware settings
80
+ """)
81
+
82
+ # Chat Interface
83
+ with gr.Row():
84
+ chatbot = gr.Chatbot(height=500)
85
+ reasoning_display = gr.Textbox(
86
+ label="Model Reasoning",
87
+ interactive=False,
88
+ visible=True,
89
+ lines=20,
90
+ max_lines=20
91
+ )
92
+
93
+ # Input Section
94
+ with gr.Row():
95
+ message = gr.Textbox(
96
+ label="Your Message",
97
+ placeholder="Type your message here...",
98
+ lines=3,
99
+ max_lines=6
100
+ )
101
+ submit_btn = gr.Button("Send", variant="primary")
102
+
103
+ # Parameters
104
+ with gr.Accordion("Normal Options", open=False):
105
+ with gr.Row():
106
+ max_length = gr.Slider(128, 4096, value=DEFAULT_MAX_LENGTH, label="Max Length")
107
+ temperature = gr.Slider(0.1, 2.0, value=DEFAULT_TEMPERATURE, label="Temperature")
108
+
109
+ with gr.Accordion("Advanced Options", open=False):
110
+ with gr.Row():
111
+ top_p = gr.Slider(0.0, 1.0, value=0.95, label="Top-p")
112
+ top_k = gr.Slider(1, 100, value=50, label="Top-k")
113
+ repetition_penalty = gr.Slider(0.1, 2.0, value=1.1, label="Repetition Penalty")
114
+ with gr.Row():
115
+ presence_penalty = gr.Slider(-2.0, 2.0, value=0.0, label="Presence Penalty")
116
+ frequency_penalty = gr.Slider(-2.0, 2.0, value=0.0, label="Frequency Penalty")
117
+
118
+ # System Prompt
119
+ system_prompt = gr.Textbox(
120
+ label="System Prompt",
121
+ value=SYSTEM_PROMPT,
122
+ lines=3
123
+ )
124
+
125
+ # Debug Options
126
+ show_reasoning = gr.Checkbox(
127
+ label="Show Model Reasoning",
128
+ value=True
129
+ )
130
+
131
+ # Event Handling
132
+ submit_btn.click(
133
+ generate_response,
134
+ inputs=[
135
+ message,
136
+ chatbot,
137
+ system_prompt,
138
+ max_length,
139
+ temperature,
140
+ top_p,
141
+ top_k,
142
+ repetition_penalty,
143
+ presence_penalty,
144
+ frequency_penalty,
145
+ show_reasoning
146
+ ],
147
+ outputs=[message, chatbot, reasoning_display]
148
+ )
149
+
150
+ message.submit(
151
+ generate_response,
152
+ inputs=[
153
+ message,
154
+ chatbot,
155
+ system_prompt,
156
+ max_length,
157
+ temperature,
158
+ top_p,
159
+ top_k,
160
+ repetition_penalty,
161
+ presence_penalty,
162
+ frequency_penalty,
163
+ show_reasoning
164
+ ],
165
+ outputs=[message, chatbot, reasoning_display]
166
+ )
167
+
168
+ # Deployment instructions
169
+ demo.launch(debug=True)
requirements.txt CHANGED
@@ -1 +1,2 @@
 
1
  huggingface_hub==0.25.2
 
1
+ gradio>=3.50
2
  huggingface_hub==0.25.2