umaradnaan commited on
Commit
cb6a853
·
verified ·
1 Parent(s): 2a26c53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -81
app.py CHANGED
@@ -1,84 +1,14 @@
1
- import streamlit as st
2
- from audiorecorder import audiorecorder
3
- import speech_recognition as sr
4
- import math
5
- import re
6
 
7
- st.set_page_config(page_title="Voice Calculator", layout="wide")
8
 
9
- st.markdown("""
10
- # 🎙️ Voice Scientific Calculator (HF Spaces Compatible)
11
- Speak natural math expressions
12
- Say **equal / equals / equal to** to calculate
13
- ---
14
- """)
15
 
16
- # ---------------- RECORD AUDIO ----------------
17
- st.subheader("🎧 Record Voice")
18
-
19
- audio = audiorecorder("🎙️ Start Recording", "⏹ Stop Recording")
20
-
21
- if len(audio) == 0:
22
- st.info("Press **Start Recording**, speak, then Stop.")
23
- else:
24
- st.audio(audio.tobytes())
25
-
26
- # ------------ SPEECH RECOGNITION -----------
27
- recognizer = sr.Recognizer()
28
-
29
- if len(audio) > 0:
30
- with st.spinner("Transcribing..."):
31
- try:
32
- audio_data = sr.AudioData(audio.tobytes(), sample_rate=44100, sample_width=2)
33
- text = recognizer.recognize_google(audio_data)
34
- except:
35
- text = ""
36
-
37
- st.success(f"🗣 Transcript: **{text}**")
38
-
39
- # ---------------- PARSER ----------------
40
- expr = text.lower()
41
-
42
- replacements = {
43
- "plus": "+",
44
- "minus": "-",
45
- "times": "*",
46
- "into": "*",
47
- "x": "*",
48
- "divide by": "/",
49
- "divided by": "/",
50
- "divide": "/",
51
- "to the power of": "**",
52
- "power": "**",
53
- "square root of": "math.sqrt",
54
- "sin": "math.sin(math.radians",
55
- "cos": "math.cos(math.radians",
56
- "tan": "math.tan(math.radians",
57
- "log": "math.log10(",
58
- "ln": "math.log(",
59
- "point": "."
60
- }
61
-
62
- # word-by-word replace
63
- for word, symbol in replacements.items():
64
- expr = expr.replace(word, symbol)
65
-
66
- # close parentheses automatically
67
- if "math.sin(math.radians" in expr: expr += ")"
68
- if "math.cos(math.radians" in expr: expr += ")"
69
- if "math.tan(math.radians" in expr: expr += ")"
70
- if "math.log10(" in expr: expr += ")"
71
- if "math.log(" in expr: expr += ")"
72
-
73
- # cleanup
74
- expr = re.sub(r"[^0-9+\-*/.()a-z* ]", "", expr)
75
- st.code(expr, language="python")
76
-
77
- # ---------------- CALCULATE ----------------
78
- if "equal" in expr:
79
- expr = expr.replace("equal","")
80
- try:
81
- result = eval(expr, {"math": math, "__builtins__": {}})
82
- st.success(f"📊 Result: **{result}**")
83
- except Exception as e:
84
- st.error(f"Error: {e}")
 
1
+ import gradio as gr
2
+ from transformers import pipeline
 
 
 
3
 
4
+ captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
5
 
6
+ def caption(image):
7
+ return captioner(image)[0]["generated_text"]
 
 
 
 
8
 
9
+ gr.Interface(
10
+ fn=caption,
11
+ inputs=gr.Image(type="pil"),
12
+ outputs="text",
13
+ title="Image Caption Generator"
14
+ ).launch()