sinatayebati commited on
Commit
276b8cf
1 Parent(s): c6872be

#7 talking duck v0.1 committed

Browse files
Files changed (4) hide show
  1. Dockerfile +1 -1
  2. app.py +72 -6
  3. docker-compose.yml +9 -0
  4. helper.txt +19 -0
Dockerfile CHANGED
@@ -14,4 +14,4 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
14
  COPY . .
15
 
16
  # Specify the command to run on container start
17
- CMD ["python", "app.py", "--host", "0.0.0.0", "--port", "7860"]
 
14
  COPY . .
15
 
16
  # Specify the command to run on container start
17
+ CMD ["python", "app.py"]
app.py CHANGED
@@ -4,7 +4,7 @@ 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):
@@ -18,23 +18,88 @@ def transcribe_audio(audio_file):
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")
@@ -45,8 +110,9 @@ with gr.Blocks() as app:
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
 
 
4
  import os
5
 
6
  API_TOKEN = os.getenv("HF_API_TOKEN")
7
+ TRANSCRIBE_API_URL = "https://api-inference.huggingface.co/models/openai/whisper-base.en"
8
  LLM_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-v0.1"
9
 
10
  def transcribe_audio(audio_file):
 
18
 
19
  def get_answer(context, question):
20
  """Get an answer from the LLM based on the context and question."""
21
+ prompt = (
22
+ "As an intelligent coding assistant, your task is to provide clear, concise, and accurate answers to coding-related questions. "
23
+ "Below are examples of questions and the kind of direct answers expected:\n\n"
24
+ "Example Question 1: How can I remove duplicates from a list in Python?\n"
25
+ "Example Answer 1: Use the set() function to convert the list to a set, which removes duplicates, then convert it back to a list.\n\n"
26
+ "Example Question 2: What's the difference between '==' and '===' in JavaScript?\n"
27
+ "Example Answer 2: '==' checks for equality of values after type coercion, while '===' checks for both value and type equality without coercion.\n\n"
28
+ "Example Question 3: How to check if a key exists in a dictionary in Python?\n"
29
+ "Example Answer 3: Use the 'in' keyword, like 'if key in my_dict:'.\n\n"
30
+ "Based on the above examples, answer the following question:\n\n"
31
+ f"Question: {question}\n"
32
+ "Answer:"
33
+ )
34
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
35
+
36
+ # Adjust generation parameters for more focused and relevant responses
37
+ payload = {
38
+ "inputs": prompt,
39
+ "parameters": {
40
+ "temperature": 0.3, # More deterministic
41
+ "top_p": 0.95, # Consider top 90% probable tokens at each step
42
+ "max_new_tokens": 100, # Limit the response length
43
+ "repetition_penalty": 1.2, # Discourage repetition
44
+ "num_return_sequences": 1, # Number of responses to generate
45
+ "return_full_text": False, # Return only generated text, not the full prompt
46
+ "top_k" : 50,
47
+ "truncate" : 24576,
48
+ "max_new_tokens" : 8192,
49
+ "stop" : ["</s>"]
50
+ },
51
+ "options": {
52
+ "use_cache": True # Use cached responses when available
53
+ }
54
+ }
55
+
56
+ response = requests.post(LLM_API_URL, headers=headers, json=payload)
57
  answer = json.loads(response.content.decode("utf-8"))[0].get("generated_text", "Answer not available")
58
  return answer
59
 
60
+
61
  def transcribe_and_answer(audio_file, question):
62
  """Process the audio file for transcription and use the result to get an answer to a question."""
63
  transcription = transcribe_audio(audio_file)
64
  answer = get_answer(transcription, question)
65
  return transcription, answer
66
 
67
+ # Create the Gradio app
68
+ import gradio as gr
69
+
70
  # Create the Gradio app
71
  with gr.Blocks() as app:
72
+ gr.HTML("""
73
+ <div style="display: flex; align-items: center; justify-content: center; margin-bottom: 20px;">
74
+ <img src="https://huggingface.co/spaces/sinatayebati/Talking-Duck/resolve/main/assets/talking-duck-logo.webp" alt="Talking Duck Logo" style="width: 120px;"/>
75
+ <div style="margin-left: 20px;">
76
+ <h1 style="font-weight: bold; font-size: 32px; margin: 0;">TALKING DUCK</h1>
77
+ <h3 style="margin: 0;">An Audio to Text Q&A Chatbot</h3>
78
+ </div>
79
+ </div>
80
+ <p style="text-align: center;">Your swift coding sidekick. Speak your code queries, and let the duck do the magic.</p>
81
+ """)
82
+
83
+ gr.Markdown("""
84
+ <div style="background-color: #0A192F; color: white; padding: 20px; border-radius: 10px; margin-bottom: 20px;">
85
+ <div style="font-size: 16px; font-weight: bold; text-align: center; margin-bottom: 10px;">Models running on backend</div>
86
+ <div style="display: flex; justify-content: space-around; align-items: center;">
87
+ <div>
88
+ <img src="https://huggingface.co/datasets/huggingchat/models-logo/resolve/main/mistral-logo.png" alt="Mistral Logo" style="width: 40px; margin-bottom: 10px;"/>
89
+ <div style="font-size: 14px;">mistralai/Mistral-7B-v0.1</div>
90
+ <a href="https://huggingface.co/mistralai/Mistral-7B-v0.1" target="_blank" style="color: white; text-decoration: none; font-size: 12px;">Model Page</a>
91
+ </div>
92
+ <div>
93
+ <img src="https://aeiljuispo.cloudimg.io/v7/https://cdn-uploads.huggingface.co/production/uploads/1620805164087-5ec0135ded25d76864d553f1.png?w=200&h=200&f=face" alt="Second Model Logo" style="width: 40px; margin-bottom: 10px;"/>
94
+ <div style="font-size: 14px;">openai/whisper-base.en</div>
95
+ <a href="https://huggingface.co/openai/whisper-base.en" target="_blank" style="color: white; text-decoration: none; font-size: 12px;">Model Page</a>
96
+ </div>
97
+ </div>
98
+ </div>
99
+ """)
100
+
101
+
102
  with gr.Row():
 
103
  audio_input = gr.Audio(type="filepath", label="Upload your audio question")
104
  question_input = gr.Textbox(label="Type your question here")
105
  answer_button = gr.Button("Get Answer")
 
110
  answer_button.click(transcribe_and_answer, inputs=[audio_input, question_input], outputs=[transcription_output, answer_output])
111
 
112
 
113
+
114
  if __name__ == "__main__":
115
+ app.launch(server_name="0.0.0.0")
116
 
117
 
118
 
docker-compose.yml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # Example docker-compose.yml for Gradio app
2
+ version: '3'
3
+ services:
4
+ gradio-app:
5
+ build: .
6
+ ports:
7
+ - "7860:7860"
8
+ environment:
9
+ - HF_API_TOKEN
helper.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ensure Docker Buildx is used and setup
2
+ docker buildx create --use
3
+
4
+ # Build your Docker image with Buildx (assuming you're now working locally and not pushing to a registry)
5
+ docker buildx build --load --secret id=HF_API_TOKEN,env=HF_API_TOKEN -t gradio-app .
6
+
7
+ # Now run your Docker container, passing the secret environment variable
8
+ docker run -it -p 7860:7860 -e HF_API_TOKEN=$HF_API_TOKEN gradio-app
9
+
10
+
11
+ docker build -t gradio-app .
12
+
13
+ docker run -it -p 7860:7860 -e HF_API_TOKEN=$HF_API_TOKEN gradio-app
14
+
15
+ docker-compose up --build
16
+
17
+
18
+
19
+ "--host", "0.0.0.0", "--port", "7860"