Toshi94 commited on
Commit
1612fc8
·
verified ·
1 Parent(s): 4e802ed

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -63
app.py CHANGED
@@ -1,70 +1,64 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- 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
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
 
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = 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
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
 
 
63
  with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
67
-
 
 
 
 
 
 
 
 
 
 
68
 
69
- if __name__ == "__main__":
70
- demo.launch()
 
1
+ # app.py
2
  import gradio as gr
 
3
 
4
+ # Conversion function
5
+ def convert_units(value, conversion_type, from_unit, to_unit):
6
+ try:
7
+ value = float(value)
8
+ except ValueError:
9
+ return "Please enter a numeric value"
10
+
11
+ # Weight conversions
12
+ weight_units = {"kg": 1, "gram": 1000, "lb": 2.20462}
13
+ if conversion_type == "Weight":
14
+ return round(value * weight_units[to_unit] / weight_units[from_unit], 4)
15
+
16
+ # Length conversions
17
+ length_units = {"meter": 1, "cm": 100, "feet": 3.28084, "inch": 39.3701}
18
+ if conversion_type == "Length":
19
+ return round(value * length_units[to_unit] / length_units[from_unit], 4)
20
+
21
+ # Temperature conversions
22
+ if conversion_type == "Temperature":
23
+ if from_unit == "Celsius" and to_unit == "Fahrenheit":
24
+ return round((value * 9/5) + 32, 2)
25
+ elif from_unit == "Fahrenheit" and to_unit == "Celsius":
26
+ return round((value - 32) * 5/9, 2)
27
+ elif from_unit == to_unit:
28
+ return value
29
+
30
+ return "Conversion not supported"
31
 
32
+ # Dropdown options
33
+ conversion_types = ["Weight", "Length", "Temperature"]
34
+ weight_units = ["kg", "gram", "lb"]
35
+ length_units = ["meter", "cm", "feet", "inch"]
36
+ temperature_units = ["Celsius", "Fahrenheit"]
 
 
 
 
 
 
 
 
37
 
38
+ # Function to get units based on type
39
+ def get_units(conversion_type):
40
+ if conversion_type == "Weight":
41
+ return gr.update(choices=weight_units, value="kg"), gr.update(choices=weight_units, value="gram")
42
+ elif conversion_type == "Length":
43
+ return gr.update(choices=length_units, value="meter"), gr.update(choices=length_units, value="cm")
44
+ elif conversion_type == "Temperature":
45
+ return gr.update(choices=temperature_units, value="Celsius"), gr.update(choices=temperature_units, value="Fahrenheit")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ # Gradio Interface
48
  with gr.Blocks() as demo:
49
+ gr.Markdown("# 🌐 Unit Conversion App")
50
+
51
+ conversion_type = gr.Dropdown(conversion_types, label="Conversion Type", value="Weight")
52
+ from_unit = gr.Dropdown(weight_units, label="From Unit", value="kg")
53
+ to_unit = gr.Dropdown(weight_units, label="To Unit", value="gram")
54
+ value_input = gr.Textbox(label="Value to Convert", value="1")
55
+ result_output = gr.Textbox(label="Converted Value")
56
+ convert_button = gr.Button("Convert")
57
+
58
+ # Update unit dropdowns dynamically
59
+ conversion_type.change(fn=get_units, inputs=conversion_type, outputs=[from_unit, to_unit])
60
+
61
+ # Convert on button click
62
+ convert_button.click(fn=convert_units, inputs=[value_input, conversion_type, from_unit, to_unit], outputs=result_output)
63
 
64
+ demo.launch()