Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Commit 
							
							·
						
						bd51214
	
1
								Parent(s):
							
							26e469b
								
doc: add more doc on env.example and rename stt, remove instrumentation if not locally run
Browse files- app.py +8 -5
- env.example +3 -1
- multiagents.py +2 -2
- tools/stt.py +7 -3
    	
        app.py
    CHANGED
    
    | @@ -11,8 +11,11 @@ from multiagents import MultiAgent | |
| 11 | 
             
            from phoenix.otel import register
         | 
| 12 | 
             
            from openinference.instrumentation.smolagents import SmolagentsInstrumentor
         | 
| 13 |  | 
| 14 | 
            -
            register | 
| 15 | 
            -
             | 
|  | |
|  | |
|  | |
| 16 |  | 
| 17 | 
             
            # (Keep Constants as is)
         | 
| 18 | 
             
            # --- Constants ---
         | 
| @@ -21,14 +24,13 @@ load_dotenv() | |
| 21 |  | 
| 22 | 
             
            max_questions = 20
         | 
| 23 |  | 
| 24 | 
            -
             | 
| 25 | 
             
            def run_and_submit_all(nb_questions: int, profile: gr.OAuthProfile | None): 
         | 
| 26 | 
             
                """
         | 
| 27 | 
             
                Fetches all questions, runs my Agent on them, submits all answers,
         | 
| 28 | 
             
                and displays the results.
         | 
| 29 | 
             
                """
         | 
| 30 | 
             
                # --- Determine HF Space Runtime URL and Repo URL ---
         | 
| 31 | 
            -
                space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
         | 
| 32 |  | 
| 33 | 
             
                if profile:
         | 
| 34 | 
             
                    username= f"{profile.username}"
         | 
| @@ -96,8 +98,9 @@ def run_and_submit_all(nb_questions: int, profile: gr.OAuthProfile | None): | |
| 96 | 
             
                        agent_question = question_text
         | 
| 97 | 
             
                        if file_question_url:
         | 
| 98 | 
             
                            agent_question += f"\n\nFile URL: {file_question_url}"
         | 
| 99 | 
            -
             | 
| 100 | 
             
                        submitted_answer = agent(agent_question)
         | 
|  | |
| 101 | 
             
                        answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
         | 
| 102 | 
             
                        results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
         | 
| 103 | 
             
                    except Exception as e:
         | 
|  | |
| 11 | 
             
            from phoenix.otel import register
         | 
| 12 | 
             
            from openinference.instrumentation.smolagents import SmolagentsInstrumentor
         | 
| 13 |  | 
| 14 | 
            +
            # use space_host var to determine if running in HF space or locally, if so register local instrumentation
         | 
| 15 | 
            +
            space_host_startup = os.getenv("SPACE_HOST")
         | 
| 16 | 
            +
            if not space_host_startup:
         | 
| 17 | 
            +
                register()
         | 
| 18 | 
            +
                SmolagentsInstrumentor().instrument()
         | 
| 19 |  | 
| 20 | 
             
            # (Keep Constants as is)
         | 
| 21 | 
             
            # --- Constants ---
         | 
|  | |
| 24 |  | 
| 25 | 
             
            max_questions = 20
         | 
| 26 |  | 
|  | |
| 27 | 
             
            def run_and_submit_all(nb_questions: int, profile: gr.OAuthProfile | None): 
         | 
| 28 | 
             
                """
         | 
| 29 | 
             
                Fetches all questions, runs my Agent on them, submits all answers,
         | 
| 30 | 
             
                and displays the results.
         | 
| 31 | 
             
                """
         | 
| 32 | 
             
                # --- Determine HF Space Runtime URL and Repo URL ---
         | 
| 33 | 
            +
                space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code   
         | 
| 34 |  | 
| 35 | 
             
                if profile:
         | 
| 36 | 
             
                    username= f"{profile.username}"
         | 
|  | |
| 98 | 
             
                        agent_question = question_text
         | 
| 99 | 
             
                        if file_question_url:
         | 
| 100 | 
             
                            agent_question += f"\n\nFile URL: {file_question_url}"
         | 
| 101 | 
            +
             | 
| 102 | 
             
                        submitted_answer = agent(agent_question)
         | 
| 103 | 
            +
                        
         | 
| 104 | 
             
                        answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
         | 
| 105 | 
             
                        results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
         | 
| 106 | 
             
                    except Exception as e:
         | 
    	
        env.example
    CHANGED
    
    | @@ -1 +1,3 @@ | |
| 1 | 
            -
            HF_TOKEN=<<your hf token>>
         | 
|  | |
|  | 
|  | |
| 1 | 
            +
            HF_TOKEN=<<your hf token>>
         | 
| 2 | 
            +
            SPACE_ID=<<your hf sapce id>>
         | 
| 3 | 
            +
            OPENAI_API_KEY=<<your open ai key>>
         | 
    	
        multiagents.py
    CHANGED
    
    | @@ -7,7 +7,7 @@ from smolagents import OpenAIServerModel | |
| 7 | 
             
            from tools.fetch import fetch_webpage, search_web
         | 
| 8 | 
             
            from smolagents import PythonInterpreterTool
         | 
| 9 | 
             
            from tools.yttranscript import get_youtube_transcript, get_youtube_title_description
         | 
| 10 | 
            -
            from tools.stt import  | 
| 11 | 
             
            from tools.image import analyze_image
         | 
| 12 | 
             
            from tools.mylogger import save_file_with_timestamp, mylog
         | 
| 13 | 
             
            import myprompts
         | 
| @@ -70,7 +70,7 @@ audiovideo_agent = CodeAgent( | |
| 70 | 
             
                tools=[
         | 
| 71 | 
             
                    get_youtube_transcript,
         | 
| 72 | 
             
                    get_youtube_title_description,
         | 
| 73 | 
            -
                     | 
| 74 | 
             
                    analyze_image
         | 
| 75 | 
             
                ],
         | 
| 76 | 
             
                name="audiovideo_agent",
         | 
|  | |
| 7 | 
             
            from tools.fetch import fetch_webpage, search_web
         | 
| 8 | 
             
            from smolagents import PythonInterpreterTool
         | 
| 9 | 
             
            from tools.yttranscript import get_youtube_transcript, get_youtube_title_description
         | 
| 10 | 
            +
            from tools.stt import get_text_transcript_from_audio_file
         | 
| 11 | 
             
            from tools.image import analyze_image
         | 
| 12 | 
             
            from tools.mylogger import save_file_with_timestamp, mylog
         | 
| 13 | 
             
            import myprompts
         | 
|  | |
| 70 | 
             
                tools=[
         | 
| 71 | 
             
                    get_youtube_transcript,
         | 
| 72 | 
             
                    get_youtube_title_description,
         | 
| 73 | 
            +
                    get_text_transcript_from_audio_file,
         | 
| 74 | 
             
                    analyze_image
         | 
| 75 | 
             
                ],
         | 
| 76 | 
             
                name="audiovideo_agent",
         | 
    	
        tools/stt.py
    CHANGED
    
    | @@ -5,9 +5,11 @@ import subprocess | |
| 5 | 
             
            import requests
         | 
| 6 | 
             
            import uuid
         | 
| 7 | 
             
            from smolagents import tool
         | 
|  | |
|  | |
| 8 |  | 
| 9 | 
             
            @tool
         | 
| 10 | 
            -
            def  | 
| 11 | 
             
                """
         | 
| 12 | 
             
                Convert speech to text using local whisper model.
         | 
| 13 | 
             
                This function downloads an audio file from a given URL, converts it to WAV format if necessary,
         | 
| @@ -23,7 +25,9 @@ def stt(file_url: str, language: str = "en-US") -> str: | |
| 23 |  | 
| 24 | 
             
                file_name = uuid.uuid4().hex +".mp3"
         | 
| 25 |  | 
| 26 | 
            -
                dest_folder = " | 
|  | |
|  | |
| 27 | 
             
                file_path = os.path.join(dest_folder + "\\tmp", file_name)
         | 
| 28 | 
             
                # 1. download the file from url (in pure python without wget or curl)
         | 
| 29 | 
             
                if not os.path.exists(file_name):            
         | 
| @@ -64,5 +68,5 @@ def stt(file_url: str, language: str = "en-US") -> str: | |
| 64 | 
             
                return text
         | 
| 65 |  | 
| 66 | 
             
            if __name__ == "__main__":
         | 
| 67 | 
            -
                transcript =  | 
| 68 | 
             
                print(transcript)   
         | 
|  | |
| 5 | 
             
            import requests
         | 
| 6 | 
             
            import uuid
         | 
| 7 | 
             
            from smolagents import tool
         | 
| 8 | 
            +
            import dotenv
         | 
| 9 | 
            +
            dotenv.load_dotenv()
         | 
| 10 |  | 
| 11 | 
             
            @tool
         | 
| 12 | 
            +
            def get_text_transcript_from_audio_file(file_url: str, language: str = "en-US") -> str:
         | 
| 13 | 
             
                """
         | 
| 14 | 
             
                Convert speech to text using local whisper model.
         | 
| 15 | 
             
                This function downloads an audio file from a given URL, converts it to WAV format if necessary,
         | 
|  | |
| 25 |  | 
| 26 | 
             
                file_name = uuid.uuid4().hex +".mp3"
         | 
| 27 |  | 
| 28 | 
            +
                dest_folder = os.getenv("STT_FOLDER")
         | 
| 29 | 
            +
                if not dest_folder:
         | 
| 30 | 
            +
                    dest_folder = '.'
         | 
| 31 | 
             
                file_path = os.path.join(dest_folder + "\\tmp", file_name)
         | 
| 32 | 
             
                # 1. download the file from url (in pure python without wget or curl)
         | 
| 33 | 
             
                if not os.path.exists(file_name):            
         | 
|  | |
| 68 | 
             
                return text
         | 
| 69 |  | 
| 70 | 
             
            if __name__ == "__main__":
         | 
| 71 | 
            +
                transcript = get_text_transcript_from_audio_file("https://agents-course-unit4-scoring.hf.space/files/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3", )
         | 
| 72 | 
             
                print(transcript)   
         | 
