Hyprlyf commited on
Commit
42e4a76
·
verified ·
1 Parent(s): f83c297

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -76
app.py CHANGED
@@ -1,84 +1,24 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import torch
4
 
5
- # Load model
6
- model_name = "apple/FastVLM-0.5B"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name)
9
 
10
- if tokenizer.pad_token is None:
11
- tokenizer.pad_token = tokenizer.eos_token
12
-
13
- def generate_response(user_input):
14
- # Fixed prompt template
15
- fixed_prompt = """
16
- You are Hypr, an artificial intelligence assistant.
17
- Your goal is to help users understand their surroundings, manage their lives better, and make decisions that improve life for future generations.
18
-
19
- When responding to a user's question:
20
- - Answer clearly, completely, and in full sentences.
21
- - Do not omit important words or information.
22
- - Keep your tone friendly, intelligent, and helpful.
23
- - If unsure, provide the most likely answer based on context.
24
- - Avoid unnecessary repetition or irrelevant details.
25
-
26
- User question: {user_input}
27
- Hypr response:
28
-
29
- """
30
-
31
- # Combine fixed prompt + user input
32
- full_prompt = f"{fixed_prompt}\n\n### Instruction:\n{user_input}\n\n### Response:\n"
33
-
34
- inputs = tokenizer.encode(full_prompt, return_tensors="pt")
35
-
36
- with torch.no_grad():
37
- outputs = model.generate(
38
- inputs,
39
- max_length=400, # ✅ REDUCED from 400 to 150
40
- temperature=0.7,
41
- do_sample=True,
42
- pad_token_id=tokenizer.eos_token_id,
43
- repetition_penalty=1.15, # ✅ INCREASED to reduce repetition
44
- top_p=0.9,
45
- top_k=50, # ✅ ADDED top-k sampling
46
- no_repeat_ngram_size=2, # ✅ PREVENT repeating phrases
47
- early_stopping=True # ✅ STOP early when complete
48
- )
49
-
50
- full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
51
-
52
- # Extract only the response part
53
- if "### Response:" in full_text:
54
- response = full_text.split("### Response:")[1].strip()
55
- if "### Instruction:" in response:
56
- response = response.split("### Instruction:")[0].strip()
57
- else:
58
- response = full_text
59
-
60
- return response
61
 
62
  # Gradio interface
63
  iface = gr.Interface(
64
- fn=generate_response,
65
- inputs=gr.Textbox(
66
- label="Ask Hypr AI",
67
- placeholder="Hello Hypr, can you help me with...",
68
- lines=3
69
- ),
70
- outputs=gr.Textbox(
71
- label="Hypr's Response",
72
- lines=6,
73
- show_copy_button=True
74
- ),
75
- title="🤖 Hypr AI Assistant",
76
- description="Meet Hypr - Your helpful AI assistant ready to help with any task!",
77
- examples=[
78
- ["Can you help me solve this math problem: 2 + 2 = ?"],
79
- ["What is the capital of France?"],
80
- ["How to make a cup of tea?"]
81
- ]
82
  )
83
 
84
- iface.launch(share=False, debug=True)
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
3
 
4
+ # Load MMS-TTS pipeline
5
+ tts = pipeline("text-to-speech", model="facebook/mms-tts")
 
 
6
 
7
+ # Function to convert text to speech
8
+ def text_to_speech(text):
9
+ audio = tts(text)
10
+ # Save audio to file for Gradio output
11
+ with open("output.wav", "wb") as f:
12
+ f.write(audio["audio"])
13
+ return "output.wav"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # Gradio interface
16
  iface = gr.Interface(
17
+ fn=text_to_speech,
18
+ inputs=gr.Textbox(lines=2, placeholder="Type your text here..."),
19
+ outputs=gr.Audio(type="filepath"),
20
+ title="Multilingual TTS with MMS-TTS",
21
+ description="Type text in any supported language and get speech output."
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  )
23
 
24
+ iface.launch()