genaibeauty commited on
Commit
d11c779
·
verified ·
1 Parent(s): 38cb315

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -47
app.py CHANGED
@@ -1,52 +1,71 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Create a pipeline with the GPT-2 model (openai-community/gpt2)
5
- pipe = pipeline("text-generation", model="openai-community/gpt2")
6
-
7
- def generate_mermaid_code(text_description):
8
- """
9
- Generate Mermaid.js code from a text description using the GPT-2 model.
10
- Args:
11
- text_description (str): A textual description of the process or flow.
12
- Returns:
13
- str: Generated Mermaid.js code.
14
- """
15
- # Define the conversation with the user as a message
16
- messages = [
17
- {"role": "user", "content": f"Convert the following process description into Mermaid.js code: {text_description}"},
18
- ]
19
-
20
- # Use the model to generate Mermaid.js code
21
- generated_text = pipe(messages[0]['content'], max_length=150, num_return_sequences=1)[0]['generated_text']
22
-
23
- # Return the generated Mermaid.js code
24
- return generated_text.strip()
25
-
26
- # Create the Gradio interface
27
- with gr.Blocks() as demo:
28
- gr.Markdown("# Process Flow to Mermaid.js Code Generator")
29
-
30
- with gr.Row():
31
- with gr.Column():
32
- # Input text description from the user
33
- text_description = gr.Textbox(
34
- label="Enter Process Description",
35
- placeholder="Describe the process flow here..."
36
- )
37
- with gr.Column():
38
- # Output Mermaid.js code
39
- mermaid_code = gr.Textbox(
40
- label="Generated Mermaid.js Code",
41
- interactive=False,
42
- placeholder="The Mermaid.js code will appear here..."
43
- )
44
-
45
- # Button to trigger Mermaid.js code generation
46
- generate_btn = gr.Button("Generate Mermaid Flowchart")
47
-
48
- # Define action on button click: generate the Mermaid code
49
- generate_btn.click(fn=generate_mermaid_code, inputs=[text_description], outputs=mermaid_code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  # Launch the Gradio app
52
- demo.launch()
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Initialize pipelines for text generation and speech recognition
5
+ text_generation_pipeline = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.2")
6
+ speech_recognition_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3-turbo")
7
+
8
+ # Function to transcribe audio using the Whisper pipeline
9
+ def transcribe_audio(audio_file):
10
+ try:
11
+ # Use the Whisper pipeline for automatic speech recognition
12
+ transcription = speech_recognition_pipeline(audio_file)
13
+ return transcription.get("text", "Transcription not available.")
14
+ except Exception as e:
15
+ return f"Error in transcription: {e}"
16
+
17
+ # Function to generate Mermaid.js code using the Mistral pipeline
18
+ def generate_mermaid_code(prompt):
19
+ try:
20
+ # Use the text-generation pipeline to generate Mermaid.js code
21
+ result = text_generation_pipeline(
22
+ prompt,
23
+ max_length=256,
24
+ temperature=0.7,
25
+ num_return_sequences=1
26
+ )
27
+ return result[0]["generated_text"].strip() if result else "No Mermaid.js code generated."
28
+ except Exception as e:
29
+ return f"Error in Mermaid.js generation: {e}"
30
+
31
+ # Function to process input (text, audio, or both)
32
+ def process_input(input_type, text_input, audio_input):
33
+ try:
34
+ if input_type == "Audio" and audio_input:
35
+ transcription = transcribe_audio(audio_input)
36
+ return generate_mermaid_code(transcription)
37
+
38
+ elif input_type == "Text" and text_input:
39
+ return generate_mermaid_code(text_input)
40
+
41
+ elif input_type == "Text and Audio" and text_input and audio_input:
42
+ transcription = transcribe_audio(audio_input)
43
+ combined_input = f"{text_input} and {transcription}"
44
+ return generate_mermaid_code(combined_input)
45
+
46
+ else:
47
+ return "Please provide valid input."
48
+ except Exception as e:
49
+ return f"Error: {e}"
50
+
51
+ # Set up the Gradio interface
52
+ iface = gr.Interface(
53
+ fn=process_input,
54
+ inputs=[
55
+ gr.Radio(["Text", "Audio", "Text and Audio"], label="Input Type", value="Text"),
56
+ gr.Textbox(lines=10, label="Text Input", placeholder="Enter task flow description here..."),
57
+ gr.Audio(type="filepath", label="Audio Input"),
58
+ ],
59
+ outputs=[
60
+ gr.Textbox(lines=20, label="Generated Mermaid.js Code"),
61
+ ],
62
+ title="Mermaid.js Generator",
63
+ description=(
64
+ "Provide text, audio, or both."
65
+ "Mermaid.js code will be generated for text or audio input, or their combination."
66
+ ),
67
+ )
68
 
69
  # Launch the Gradio app
70
+ if __name__ == "__main__":
71
+ iface.launch()