Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,14 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
-
import speech_recognition as sr
|
| 4 |
-
import math
|
| 5 |
-
import re
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
Speak natural math expressions
|
| 12 |
-
Say **equal / equals / equal to** to calculate
|
| 13 |
-
---
|
| 14 |
-
""")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|