Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import os
|
4 |
-
|
5 |
|
6 |
# Set up the Hugging Face API key (ensure you've set this as an environment variable)
|
7 |
api_key = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
8 |
|
9 |
-
# API
|
10 |
WHISPER_API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
|
|
|
|
|
11 |
|
12 |
-
# Set up headers for
|
13 |
-
headers = {"Authorization": f"Bearer {api_key}"}
|
14 |
-
|
15 |
-
# Load the DeepSeek-R1-Distill-Qwen-1.5B model using Hugging Face's pipeline
|
16 |
-
pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B")
|
17 |
|
18 |
# Function to query the Hugging Face Whisper model for audio transcription
|
19 |
def transcribe_audio(audio_file):
|
@@ -25,33 +24,47 @@ def transcribe_audio(audio_file):
|
|
25 |
else:
|
26 |
return f"Error: {response.status_code}, {response.text}"
|
27 |
|
28 |
-
# Function to
|
29 |
def generate_mermaid_code(prompt):
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Function to process text, audio, or both inputs
|
38 |
def process_input(input_type, text_input, audio_input):
|
39 |
if input_type == "Audio" and audio_input is not None:
|
40 |
-
# Transcribe audio
|
41 |
transcription = transcribe_audio(audio_input)
|
42 |
-
# Generate Mermaid.js code from transcription
|
43 |
return generate_mermaid_code(transcription)
|
44 |
|
45 |
elif input_type == "Text" and text_input:
|
46 |
-
# Generate Mermaid.js code directly from text input
|
47 |
return generate_mermaid_code(text_input)
|
48 |
|
49 |
elif input_type == "Text and Audio" and text_input and audio_input is not None:
|
50 |
-
# Transcribe audio
|
51 |
transcription = transcribe_audio(audio_input)
|
52 |
# Combine text input and transcription
|
53 |
combined_input = f"{text_input} and {transcription}"
|
54 |
-
# Generate Mermaid.js code
|
55 |
return generate_mermaid_code(combined_input)
|
56 |
|
57 |
else:
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import os
|
4 |
+
import json
|
5 |
|
6 |
# Set up the Hugging Face API key (ensure you've set this as an environment variable)
|
7 |
api_key = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
8 |
|
9 |
+
# API URLs
|
10 |
WHISPER_API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
|
11 |
+
# MISTRAL_API_URL (DeepSeek API call for generating Mermaid code)
|
12 |
+
MISTRAL_API_URL = "https://huggingface.co/api/inference-proxy/together/v1/chat/completions"
|
13 |
|
14 |
+
# Set up headers for API requests
|
15 |
+
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
|
|
|
|
|
|
16 |
|
17 |
# Function to query the Hugging Face Whisper model for audio transcription
|
18 |
def transcribe_audio(audio_file):
|
|
|
24 |
else:
|
25 |
return f"Error: {response.status_code}, {response.text}"
|
26 |
|
27 |
+
# Function to query the Hugging Face API to generate Mermaid.js code
|
28 |
def generate_mermaid_code(prompt):
|
29 |
+
# Define the payload to send to the Hugging Face API
|
30 |
+
mermaid_prompt = f"Generate a valid MermaidJS diagram code for the following: {prompt}"
|
31 |
+
|
32 |
+
payload = {
|
33 |
+
"model": "deepseek-ai/DeepSeek-R1",
|
34 |
+
"messages": [{"role": "user", "content": mermaid_prompt}],
|
35 |
+
"max_tokens": 500,
|
36 |
+
"stream": False
|
37 |
+
}
|
38 |
+
|
39 |
+
# Send the request to the Hugging Face API
|
40 |
+
response = requests.post(MISTRAL_API_URL, headers=headers, data=json.dumps(payload))
|
41 |
+
|
42 |
+
# Check if the request was successful
|
43 |
+
if response.status_code == 200:
|
44 |
+
result = response.json()
|
45 |
+
# Extract the generated Mermaid.js code from the response
|
46 |
+
return result['choices'][0]['message']['content'].strip()
|
47 |
+
else:
|
48 |
+
return f"Error: {response.status_code}, {response.text}"
|
49 |
|
50 |
# Function to process text, audio, or both inputs
|
51 |
def process_input(input_type, text_input, audio_input):
|
52 |
if input_type == "Audio" and audio_input is not None:
|
53 |
+
# Transcribe audio
|
54 |
transcription = transcribe_audio(audio_input)
|
55 |
+
# Generate Mermaid.js code from the transcription
|
56 |
return generate_mermaid_code(transcription)
|
57 |
|
58 |
elif input_type == "Text" and text_input:
|
59 |
+
# Generate Mermaid.js code directly from text input
|
60 |
return generate_mermaid_code(text_input)
|
61 |
|
62 |
elif input_type == "Text and Audio" and text_input and audio_input is not None:
|
63 |
+
# Transcribe audio
|
64 |
transcription = transcribe_audio(audio_input)
|
65 |
# Combine text input and transcription
|
66 |
combined_input = f"{text_input} and {transcription}"
|
67 |
+
# Generate Mermaid.js code from the combined input
|
68 |
return generate_mermaid_code(combined_input)
|
69 |
|
70 |
else:
|