segestic commited on
Commit
cddf55e
·
verified ·
1 Parent(s): 8433a79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -8
app.py CHANGED
@@ -1,12 +1,44 @@
1
- import os
2
  import gradio as gr
 
3
 
4
- # Access the Hugging Face token from environment variables
5
- hf_token = os.getenv("HF_TOKEN")
6
 
7
- # Load a private Hugging Face Space using the updated `token` parameter and `src='spaces'`
8
- app = gr.load(name="segestic/token-cost-calculator", src="spaces", token=hf_token)
 
 
 
 
 
 
 
 
 
9
 
10
- # Use the app
11
- output = app(["Input text"])
12
- print(output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from gradio_client import Client
3
 
4
+ # Initialize the Client for the Hugging Face Space
5
+ client = Client("segestic/token-cost-calculator")
6
 
7
+ # Function to interact with the Hugging Face Space API
8
+ def interact_with_space(system_prompt, user_prompt, assistant_response, model="gpt-4o"):
9
+ # Call the API with the provided inputs
10
+ result = client.predict(
11
+ system_prompt=system_prompt,
12
+ user_prompt=user_prompt,
13
+ assistant_response=assistant_response,
14
+ model=model,
15
+ api_name="/predict"
16
+ )
17
+ return result
18
 
19
+ # Define the Gradio interface for the UI
20
+ def create_ui():
21
+ # Create the input and output components for the UI
22
+ with gr.Blocks() as demo:
23
+ # Inputs
24
+ system_prompt_input = gr.Textbox(label="System Prompt", placeholder="Enter system prompt here", lines=2)
25
+ user_prompt_input = gr.Textbox(label="User Prompt", placeholder="Enter user prompt here", lines=2)
26
+ assistant_response_input = gr.Textbox(label="Assistant Response", placeholder="Enter assistant response here", lines=2)
27
+ model_input = gr.Textbox(label="Model", value="gpt-4o") # Default model is gpt-4o
28
+
29
+ # Output
30
+ output = gr.Textbox(label="Response", interactive=False)
31
+
32
+ # Button to send inputs to the model and get a response
33
+ submit_btn = gr.Button("Get Response")
34
+
35
+ # When the button is clicked, interact with the Space and display the result
36
+ submit_btn.click(interact_with_space,
37
+ inputs=[system_prompt_input, user_prompt_input, assistant_response_input, model_input],
38
+ outputs=[output])
39
+
40
+ return demo
41
+
42
+ # Launch the Gradio UI
43
+ ui = create_ui()
44
+ ui.launch()