openfree commited on
Commit
6c31f83
·
verified ·
1 Parent(s): 69a1259

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
4
+ import json
5
+
6
+ def create_deepseek_interface():
7
+ # Initialize chat history
8
+ chat_history = []
9
+
10
+ # Function to call the DeepSeek API
11
+ def query_deepseek(message, history, api_key):
12
+ if not api_key:
13
+ return history, "Please provide your Fireworks AI API key"
14
+
15
+ # Prepare the conversation history for the API
16
+ messages = []
17
+ for h in history:
18
+ messages.append({"role": "user", "content": h[0]})
19
+ messages.append({"role": "assistant", "content": h[1]})
20
+
21
+ # Add the new user message
22
+ messages.append({"role": "user", "content": message})
23
+
24
+ # Prepare the API request
25
+ url = "https://api.fireworks.ai/inference/v1/chat/completions"
26
+ payload = {
27
+ "model": "accounts/fireworks/models/deepseek-v3",
28
+ "max_tokens": 16384,
29
+ "top_p": 1,
30
+ "top_k": 40,
31
+ "presence_penalty": 0,
32
+ "frequency_penalty": 0,
33
+ "temperature": 0.6,
34
+ "messages": messages
35
+ }
36
+ headers = {
37
+ "Accept": "application/json",
38
+ "Content-Type": "application/json",
39
+ "Authorization": f"Bearer {api_key}"
40
+ }
41
+
42
+ try:
43
+ # Make the API request
44
+ response = requests.post(url, headers=headers, data=json.dumps(payload))
45
+ response.raise_for_status() # Raise exception for HTTP errors
46
+
47
+ # Extract the response
48
+ result = response.json()
49
+ assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
50
+
51
+ # Update history with the new exchange
52
+ history.append((message, assistant_response))
53
+ return history, ""
54
+ except requests.exceptions.RequestException as e:
55
+ error_msg = f"API Error: {str(e)}"
56
+ if response.status_code == 401:
57
+ error_msg = "Authentication failed. Please check your API key."
58
+ return history, error_msg
59
+
60
+ # Create Gradio interface
61
+ with gr.Blocks(theme="soft", fill_height=True) as demo:
62
+ # Header Section
63
+ gr.Markdown(
64
+ """
65
+ # 🤖 DeepSeek V3 Inference Interface
66
+ ### Advanced AI Model Powered by Fireworks AI
67
+ """
68
+ )
69
+
70
+ # State for error messages
71
+ error_msg = gr.State("")
72
+
73
+ # Main layout with two columns
74
+ with gr.Row():
75
+ # Sidebar with Model Information and API Key
76
+ with gr.Column(scale=1):
77
+ gr.Markdown(
78
+ """
79
+ ## 🔑 Access Control
80
+ ### Inference Provider
81
+ This interface connects to the DeepSeek-V3 model, served by the Fireworks AI API.
82
+
83
+ #### Authentication
84
+ - Enter your Fireworks AI API key below
85
+ - Secure API access with end-to-end encryption
86
+ """
87
+ )
88
+
89
+ # API Key input
90
+ api_key = gr.Textbox(
91
+ label="Fireworks AI API Key",
92
+ placeholder="Enter your API key...",
93
+ type="password"
94
+ )
95
+
96
+ # Model Details Section
97
+ gr.Markdown(
98
+ """
99
+ ### 📊 Model Details
100
+ - **Model**: DeepSeek-V3
101
+ - **Provider**: Fireworks AI
102
+ - **Max Tokens**: 16,384
103
+ - **Temperature**: 0.6
104
+ - **Capabilities**: Advanced Language Understanding
105
+ """
106
+ )
107
+
108
+ # Main Content Area
109
+ with gr.Column(scale=2):
110
+ # Chat interface
111
+ chatbot = gr.Chatbot(
112
+ height=500,
113
+ show_label=False,
114
+ container=True,
115
+ bubble=True,
116
+ avatar_images=("👤", "🤖")
117
+ )
118
+
119
+ # Error message display
120
+ error_display = gr.Markdown(visible=False)
121
+
122
+ # Input area
123
+ with gr.Row():
124
+ msg = gr.Textbox(
125
+ label="Your Message",
126
+ placeholder="Type your prompt here...",
127
+ show_label=False,
128
+ scale=9
129
+ )
130
+ submit = gr.Button("Send", variant="primary", scale=1)
131
+
132
+ # Buttons for clearing and examples
133
+ with gr.Row():
134
+ clear = gr.ClearButton([msg, chatbot], value="🧹 Clear Conversation")
135
+
136
+ # Example queries
137
+ gr.Examples(
138
+ examples=[
139
+ "Explain the differences between transformers and RNNs in deep learning.",
140
+ "Write a Python function to find prime numbers in a range.",
141
+ "Summarize the key concepts of reinforcement learning."
142
+ ],
143
+ inputs=msg
144
+ )
145
+
146
+ # Handle form submission
147
+ def process_submission(message, history, api_key):
148
+ if not message.strip():
149
+ return history, error_display.update(visible=False)
150
+
151
+ updated_history, error = query_deepseek(message, history.copy(), api_key)
152
+
153
+ if error:
154
+ return history, error_display.update(value=f"**Error:** {error}", visible=True)
155
+ else:
156
+ return updated_history, error_display.update(visible=False)
157
+
158
+ # Connect the button to the function
159
+ submit.click(
160
+ process_submission,
161
+ inputs=[msg, chatbot, api_key],
162
+ outputs=[chatbot, error_display],
163
+ postprocess=lambda _: ("", ) # Clear input box after sending
164
+ )
165
+
166
+ # Also allow pressing Enter to submit
167
+ msg.submit(
168
+ process_submission,
169
+ inputs=[msg, chatbot, api_key],
170
+ outputs=[chatbot, error_display],
171
+ postprocess=lambda _: ("", ) # Clear input box after sending
172
+ )
173
+
174
+ return demo
175
+
176
+ # Launch the interface
177
+ if __name__ == "__main__":
178
+ demo = create_deepseek_interface()
179
+ demo.launch(debug=True)