genaibeauty commited on
Commit
81858f2
·
verified ·
1 Parent(s): e1962f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -45
app.py CHANGED
@@ -1,81 +1,51 @@
1
  import gradio as gr
2
  import requests
3
  import os
 
4
 
5
  # Set up the Hugging Face API key (ensure you've set this as an environment variable)
6
  api_key = os.getenv("HUGGINGFACEHUB_API_TOKEN")
7
 
8
-
9
  # API URLs
10
  WHISPER_API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
11
- #MISTRAL_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-Nemo-Instruct-2407"
12
- MISTRAL_API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1"
13
 
14
- deepseek-ai/DeepSeek-R1
15
- # Set up headers for API requests
16
- headers = {"Authorization": f"Bearer {api_key}"}
 
 
17
 
18
- # Function to query the Hugging Face Whisper model for audio transcription
19
  def transcribe_audio(audio_file):
20
  with open(audio_file, "rb") as f:
21
  data = f.read()
22
- response = requests.post(WHISPER_API_URL, headers=headers, data=data)
23
  if response.status_code == 200:
24
  return response.json().get("text", "Transcription not available.")
25
  else:
26
  return f"Error: {response.status_code}, {response.text}"
27
 
28
- # Function to query the Mistral model to generate Mermaid.js code
29
  def generate_mermaid_code(prompt):
30
- # mermaid_prompt = f"Use the appropriate diagram type (Use Case Diagram, Flowchart, Sequence Diagram, Entity-Relationship (ER) Diagram,State Diagram, Pie Chart etc.) based on the context.\n" \
31
- # "Generate a valid, syntactically correct MermaidJS diagram code for the following: {prompt}"
32
- mermaid_prompt = f"Generate a valid MermaidJS diagram code for the following: {prompt}"
33
-
34
-
35
- # Prepare the payload (input for the model)
36
- payload = {
37
- "inputs": mermaid_prompt,
38
- "parameters": {
39
- "max_length": 256,
40
- "temperature": 0.7
41
- }
42
- }
43
-
44
- # Send the request to the Mistral API
45
- response = requests.post(MISTRAL_API_URL, headers=headers, json=payload)
46
-
47
- # Check if the request was successful
48
- if response.status_code == 200:
49
- result = response.json()
50
- # Extract the generated Mermaid.js code
51
- return result[0]['generated_text'].strip()
52
- else:
53
- return f"Error: {response.status_code}, {response.text}"
54
 
55
- # Function to process text, audio, or both inputs
56
  def process_input(input_type, text_input, audio_input):
57
  if input_type == "Audio" and audio_input is not None:
58
- # Transcribe audio
59
  transcription = transcribe_audio(audio_input)
60
- # Generate Mermaid.js code
61
  return generate_mermaid_code(transcription)
62
-
63
  elif input_type == "Text" and text_input:
64
- # Generate Mermaid.js code directly from text input
65
  return generate_mermaid_code(text_input)
66
-
67
  elif input_type == "Text and Audio" and text_input and audio_input is not None:
68
- # Transcribe audio
69
  transcription = transcribe_audio(audio_input)
70
- # Combine text input and transcription
71
  combined_input = f"{text_input} and {transcription}"
72
- # Generate Mermaid.js code
73
  return generate_mermaid_code(combined_input)
74
-
75
  else:
76
  return "No valid input provided."
77
 
78
- # Set up the Gradio interface
79
  iface = gr.Interface(
80
  fn=process_input,
81
  inputs=[
@@ -90,5 +60,4 @@ iface = gr.Interface(
90
  description="Provide text, audio, or both. Mermaid.js code will be generated based on the text or audio input, or their combination."
91
  )
92
 
93
- # Launch the Gradio app
94
  iface.launch()
 
1
  import gradio as gr
2
  import requests
3
  import os
4
+ from huggingface_hub import InferenceClient
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
 
12
+ # Set up inference client for DeepSeek-R1
13
+ client = InferenceClient(
14
+ provider="together",
15
+ api_key=api_key
16
+ )
17
 
 
18
  def transcribe_audio(audio_file):
19
  with open(audio_file, "rb") as f:
20
  data = f.read()
21
+ response = requests.post(WHISPER_API_URL, headers={"Authorization": f"Bearer {api_key}"}, data=data)
22
  if response.status_code == 200:
23
  return response.json().get("text", "Transcription not available.")
24
  else:
25
  return f"Error: {response.status_code}, {response.text}"
26
 
 
27
  def generate_mermaid_code(prompt):
28
+ messages = [{"role": "user", "content": f"Generate a valid MermaidJS diagram code for the following: {prompt}"}]
29
+ completion = client.chat.completions.create(
30
+ model="deepseek-ai/DeepSeek-R1",
31
+ messages=messages,
32
+ max_tokens=500
33
+ )
34
+ return completion.choices[0].message['content'].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
 
36
  def process_input(input_type, text_input, audio_input):
37
  if input_type == "Audio" and audio_input is not None:
 
38
  transcription = transcribe_audio(audio_input)
 
39
  return generate_mermaid_code(transcription)
 
40
  elif input_type == "Text" and text_input:
 
41
  return generate_mermaid_code(text_input)
 
42
  elif input_type == "Text and Audio" and text_input and audio_input is not None:
 
43
  transcription = transcribe_audio(audio_input)
 
44
  combined_input = f"{text_input} and {transcription}"
 
45
  return generate_mermaid_code(combined_input)
 
46
  else:
47
  return "No valid input provided."
48
 
 
49
  iface = gr.Interface(
50
  fn=process_input,
51
  inputs=[
 
60
  description="Provide text, audio, or both. Mermaid.js code will be generated based on the text or audio input, or their combination."
61
  )
62
 
 
63
  iface.launch()