sinatayebati commited on
Commit
44aba0e
·
1 Parent(s): a1388c8

#5 initial app committed

Browse files
Files changed (3) hide show
  1. Dockerfile +7 -4
  2. app.py +53 -0
  3. main.py +0 -7
Dockerfile CHANGED
@@ -1,14 +1,17 @@
1
- # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
- # you will also find guides on how best to write your Dockerfile
3
-
4
  FROM python:3.9
5
 
 
6
  WORKDIR /code
7
 
 
8
  COPY ./requirements.txt /code/requirements.txt
9
 
 
10
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
 
 
12
  COPY . .
13
 
14
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
1
+ # Base image
 
 
2
  FROM python:3.9
3
 
4
+ # Set the working directory inside the container
5
  WORKDIR /code
6
 
7
+ # Copy the dependencies file to the working directory
8
  COPY ./requirements.txt /code/requirements.txt
9
 
10
+ # Install any dependencies
11
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
12
 
13
+ # Copy the content of the local src directory to the working directory
14
  COPY . .
15
 
16
+ # Specify the command to run on container start
17
+ CMD ["python", "app.py", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import os
5
+
6
+ API_TOKEN = os.getenv("HF_API_TOKEN")
7
+ TRANSCRIBE_API_URL = "https://api-inference.huggingface.co/models/facebook/wav2vec2-base-960h"
8
+ LLM_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-v0.1"
9
+
10
+ def transcribe_audio(audio_file):
11
+ """Transcribe audio file to text."""
12
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
13
+ with open(audio_file, "rb") as f:
14
+ data = f.read()
15
+ response = requests.post(TRANSCRIBE_API_URL, headers=headers, data=data)
16
+ transcription = json.loads(response.content.decode("utf-8")).get("text", "Transcription not available")
17
+ return transcription
18
+
19
+ def get_answer(context, question):
20
+ """Get an answer from the LLM based on the context and question."""
21
+ prompt = f"Context: {context}\nQuestion: {question}\nAnswer:"
22
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
23
+ response = requests.post(LLM_API_URL, headers=headers, json={"inputs": prompt})
24
+ answer = json.loads(response.content.decode("utf-8"))[0].get("generated_text", "Answer not available")
25
+ return answer
26
+
27
+ def transcribe_and_answer(audio_file, question):
28
+ """Process the audio file for transcription and use the result to get an answer to a question."""
29
+ transcription = transcribe_audio(audio_file)
30
+ answer = get_answer(transcription, question)
31
+ return transcription, answer
32
+
33
+ # Create the Gradio app
34
+ with gr.Blocks() as app:
35
+ gr.Markdown("### Audio to Text and Q&A Chatbot")
36
+ with gr.Row():
37
+ # Corrected 'type' parameter value to 'filepath'
38
+ audio_input = gr.Audio(type="filepath", label="Upload your audio question")
39
+ question_input = gr.Textbox(label="Type your question here")
40
+ answer_button = gr.Button("Get Answer")
41
+ with gr.Row():
42
+ transcription_output = gr.Textbox(label="Transcription")
43
+ answer_output = gr.Textbox(label="Answer")
44
+
45
+ answer_button.click(transcribe_and_answer, inputs=[audio_input, question_input], outputs=[transcription_output, answer_output])
46
+
47
+
48
+ if __name__ == "__main__":
49
+ app.launch()
50
+
51
+
52
+
53
+
main.py DELETED
@@ -1,7 +0,0 @@
1
- from fastapi import FastAPI
2
-
3
- app = FastAPI()
4
-
5
- @app.get("/")
6
- def read_root():
7
- return {"Hello": "World!"}